The data is imported as .mat from .csv by using xlsread, and looks like this:
0203.ENG
0412.DXY
....
How to return the row and column of '0412.DXY'?
Thanks!
I assume you've got a cell array reading raw data by XLSREAD.
[num,txt,raw] = xlsread(filename);
Then you can find row and column of a particular string with
[r, c] = find(strcmp(raw,'0412.DXY'));
Related
I am having a simple problem while reading excel data which contains strings, long string, and numbers. Now I need to make each column (I have 11 here) to define separate variables of 1 column vector so that I can plot in MATLAB against each other or combination.
But the problem is the reading the file and creating 11 column vector. When I assign variable the header also comes.
Code:
%fid = fopen('Data_Link.xlsx');
[num,txt,raw] = xlsread('Data_Link.xlsx');
%fclose(fid);
% Extract data from readData
A = raw(:,1);
B = raw(:,2);
C = raw(:,6);
So I need the variables without header
Data file is truncated and given here.
Can anyone help me?
You can use readtable as ThP suggested. But if you want to use xlsread and you want your data without the header, you just need to remove the first row as in the below example:
%fid = fopen('Data_Link.xlsx');
[num,txt,raw] = xlsread('Data_Link.xlsx');
%fclose(fid);
% Extract data from readData
A = raw(2:end,1);
B = raw(2:end,2);
C = raw(2:end,6);
Note that each array will receive data from row 2 to last row.
You can use readtable instead of xlsread.
Using
T = readtable(‘Data_Link.xlsx’)
will result in a table with a variable for each column. For example T.Year would hold the values from the ‘Year’ column and T.Title would hold the values from the ‘Title’ column, etc.
I am trying to import the data from an excel sheet in which there are 3 columns (Time; Id; and Data).
The first column contains time (in seconds) while the next column has numbers (Id). The third column (Data), however, has hexadecimal numbers, therefore Matlab is unable to process that information and shows 'NaN' in the command window/structure whenever I try to retrieve any information from the third column.
I want to create a structure for each Ids and display the respective information inside that Id.
Hence is there anyway to obtain the information from the third column without getting NaN error?
Here's my code:
[dat1, dat2, dat3] = xlsread('1');
flds=dat3(1,:);
bus=cell2struct(dat3(2:end,:),flds,2);
for k=1:length(bus)
if bus(k).Id == 150
i=i+1;
can_bus(k,:)
end
end
You can get the raw text and do the conversion yourselves:
To get the text, you have to get other output parameters
from xlsread. For example:
[num,txt,raw] = xlsread('file.xls');
will return the numeric values in num, the text values in txt, and
the raw cell data in raw.
Source
I'm new to Matlab and I want to convert a column with space separated numbers (source is an Excel file) to a list of arrays.
In a first step I want to create a list of arrays like this:
Then I want to transpose the list like this:
Whats the correct command for this conversion?
I know it's a simple question, but I couldn't find a similar one.
First use xlsread to read in the raw text. The text will be read in as a cell array where each row of text is placed in a cell. Once you do this, it's a matter of splitting up the strings by spaces to create an additional cell array of cells per row, then inputting these cells into a function that creates an array of numbers. You can use cellfun combined with strsplit and str2double. Assuming your Excel file is called list.xls, do something like this:
[~,~,RAW] = xlsread('list.xls');
list = cellfun(#str2double, cellfun(#strsplit, RAW, 'uni', 0), 'uni', 0).';
list contains the desired output. I've also transposed the result as this is what you desire. I created an Excel file that's in the same fashion as how you've mentioned in your post. This is what I get when I run the code. First I'll show what list looks like, then we'll examine what the actual contents are:
>> list
list =
[1x4 double] [1x5 double] [1x6 double]
>> celldisp(list)
list{1} =
5405 5414 5420 9999
list{2} =
5405 5414 5430 5341 9999
list{3} =
5405 5419 5419 5419 5412 9999
Here's also what the MATLAB Variable Editor looks like:
I'm trying to convert an Excel spreadsheet (link) to a MATLAB sparse matrix.
How can I import it to MATLAB as a working sparse matrix? So far, all I've been able to do is convert it to a cell array...
I tried to convert my name matrix first with this function:
function [names,links]=Changing(WorldReligion)
i=0;
names={};
for i=1:337
if WorldReligion(i,1)~=WorldReligion(i+1,1)
names(i)=WorldReligion(i,1);
end
end
However, I get this error:
[names , links]=Changing(WorldReligion) Undefined function 'ne' for
input arguments of type 'cell'.
Any help would be appreciated.
That's a short answer because it looked bad as a comment, but if you want to fetch the unique religion names from your spreadsheet, you can use the unique function which works on cell arrays of strings. In your case to get the 1st column of the spreadsheet, use this:
clear
clc
[~, text, ~] = xlsread('WorldReligion.xlsx');
UniqueNames = unique(text(:,1));
whos
Output of whos:
Name Size Bytes Class Attributes
UniqueNames 117x1 15654 cell
text 388x10 452500 cell
This retrieves the 117 unique names form the 1st column of the 'text' data, which I think is what you wanted to do with the above for-loop. Is that the case?
I have a number of dates in an Excel file associated with some other numbers, then I want to import this data in Matlab.
E.g. I have 5/17/06 on Excel and in Matlab it appears as 0.0490196078431373
when I try to import it..
How can I import the dates correctly into matlab??
Thank you in advance!
Here is an idea of what you need to do.
[A B C] = xlsread('C:\Users\Admin\Desktop\test.xlsx') %This is just a dummy file I made to test with.
B will contain the data from the excel file. It is VERY important to note that it will be the exact same as the layout from excel. This is what I have in the dummy file:
5/17/06 asd 12
5/18/06 s sd
5/19/06 asd asd
5/20/06 dsd sd
5/21/06 e2 asd
So the resulting matrix B will be a cell array with 3 columns and 5 rows. You can then pull the date out into a separate matrix like so:
date = B(:,1)
Now date is a vector of cells that contain the dates you had before. You can then apply other functions to convert to a char array (string) if you want to, etc.