Handling excel file

4 views (last 30 days)
Changqing Xu
Changqing Xu on 24 Feb 2011
I need to read an excel formated data into Matlab. This data file contains 33757x50 cells, and there are several nonconsecutive rows and columns that hold characters. What I need finally is a sub-sheet, say, the 3,7,10,11,14,19,25,27,31,...,232,300th rows from the original sheet of the excel file. the excel read command >>xlsread(filename,sheet,range) does not have such kind of function since the option 'range' should be consecutive, e.g., B2:F50. Any good suggestions are appreciated!
Richard
  1 Comment
Walter Roberson
Walter Roberson on 26 Feb 2011
It sounds to me as if something like a mysql database would be more appropriate for your needs.

Sign in to comment.

Answers (3)

Matt Tearle
Matt Tearle on 24 Feb 2011
Why not import everything, then strip out any blank rows?
[~,~,x] = xlsread('foo.xls');
x(all(cellfun(@isempty,x),2),:) = [];
  4 Comments
Changqing Xu
Changqing Xu on 27 Feb 2011
Thanks! But this does not work since the excel sheet contains different classes of data (e.g. char and numeric), and who is char and who is numerical is not so clear. The first job we shall do, I think, is to separate the char from the numerical data and save them by different variables which obviously cannot be fulfilled by xlsread; Based upon some analysis on the numerical part, we then come to the original sheet and choose some rows to form the sub-sheet, which is our destination.
Matt Tearle
Matt Tearle on 27 Feb 2011
See Chris Hinkle's comments. x in my example is a cell array of everything, so the indexing I showed works just fine, as long as you know what idx is. You could also investigate using the is* functions and/or cellfun. E.g. c(~cellfun(@isnumeric,c(:,1)),:) = []; would remove any rows where the first column is not numeric.

Sign in to comment.


Oleg Komarov
Oleg Komarov on 26 Feb 2011
Other than importing the entire excel as Matt suggest (which I would go for unless the number of rows selected is just a tiny part of a huge excel), it would require to go to the low level using actxserver.
Then you can select specific ranges as this tutorial shows: How to select cells/ranges by using Visual Basic procedures in Excel
Oleg

chris hinkle
chris hinkle on 27 Feb 2011
Some tips
1. Xlsread allows you to bring in raw numeric and text [Numeric text raw] = xlsread() so everything will be parsed for you
So once you have this you will see numeric is an array of doubles and text is a cell array of strings while raw is a cell array of mixed types
2. Depending on your selection criteria you need to use the find command with criteria for numbers or if for example you wanted to get rid of rows with empty fields get the indices by using strcmp, this DOES work with cell array of strings. Hope this helps

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!