Handling excel file

1 次查看(过去 30 天)
Changqing Xu
Changqing Xu 2011-2-24
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 个评论
Walter Roberson
Walter Roberson 2011-2-26
It sounds to me as if something like a mysql database would be more appropriate for your needs.

请先登录,再进行评论。

回答(3 个)

Matt Tearle
Matt Tearle 2011-2-24
Why not import everything, then strip out any blank rows?
[~,~,x] = xlsread('foo.xls');
x(all(cellfun(@isempty,x),2),:) = [];
  4 个评论
Changqing Xu
Changqing Xu 2011-2-27
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 2011-2-27
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.

请先登录,再进行评论。


Oleg Komarov
Oleg Komarov 2011-2-26
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 2011-2-27
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!

Translated by