problem with xlsread:Bizarre results
Show older comments
Dear all,
I have an excel file that contains 3 sheets
when I look at each of these sheets inside the excel file they all have the same number of rows( 245) but when I use the command
[num1 ,txt1 ,raw1 ] = xlsread(xlfilename,1);
[num2 ,txt2 ,raw2 ] = xlsread(xlfilename,2);
[num3 ,txt3 ,raw3 ] = xlsread(xlfilename,3);
I get that raw1 has 266 rows
raw2 has 267 rows
raw3 has 279 rows
I also noticed that inside the workspace matlab adds arbitrarily different number of rows of NaN at the end of the raw1 raw2 raw3
Bear also in mind that “raw1” “raw2” “raw3” contains string numbers and empty cells
Unfortunately it is not convenient for my purposes to use “num1““num1“ “num1“
Thank you
Accepted Answer
More Answers (1)
F.
on 4 Jul 2012
Hello,
I think the problem is with Excel.
I think you should have at a moment more than 245 lines, and you should have deleted them. You don't see these old lines in excel, but the application remains it. When you are using xlsread, you import these empty lines. You can see this with raw datas.
When you want import data from excel, 2 solutions:
- First, in excel you select all lines ( and after all columns ) after your data and you remove them (not only delete)
- !the other one, with raw data you can define lines and columns with no data or NaN data, and you delete them :
[ N, T, R ] = xlsread( ‘toto.xls’ );
Tmp = cellfun( @isnan, R, ‘UniformOuput’, true);
R( all( Tmp,1), : ) = [] ;
R( : , all( Tmp,2) ) = [] ;
Or something like this (I’m sorry but I don’t have Matlab on my Pc today) (it’s the same thing with “empty” )
8 Comments
Sabbas
on 4 Jul 2012
F.
on 4 Jul 2012
I don't undersand your comment. If you want just erase rows from your raw data :
Tmp = cellfun( @isnan, R, 'UniformOutput', true)
R(all(Tmp,2),: )=[]
but if you have some rows with NaN in your data and you can use :
Tmp = cellfun( @isnan, R, 'UniformOutput', true)
Tmp = find( ~all(Tmp,2),1, 'last')
R((Tmp+1):end,: )=[]
otherwise, I'm sorry, I didn't undersand your problem because, for me, it's just a problem with NaN rows at the end of your data ...
Sabbas
on 4 Jul 2012
F.
on 5 Jul 2012
The reason is some cells contain string. Try this :
Tmp = cellfun( @(C) all(isnan(C)), R, 'UniformOutput', true)
Mark Whirdy
on 5 Jul 2012
or
Tmp = cellfun( @isnan, R, 'UniformOutput', false)
as per error?
Need to start looking at the data and removing unncessary rows with cellfun(@isnumeric, cellfun(@isnan etc etc. Just experiment
F.
on 5 Jul 2012
If you use this command, you will have a cell array for "Tmp", and we need an array.
Sabbas
on 5 Jul 2012
Mark Whirdy
on 9 Jul 2012
cell2mat
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!