Read a certain amount of rows from a column of an excel file
古いコメントを表示

I need matlab to start taking values at row 3 and run through the column to row 367. Afterwards, I need it to read 368 through 732, and so on. So I need it to read the column in sections of 365, without the user knowing how many times.
I know that:
[num, ~, ~] = xlsread('datafile');
x = num(3:367,4);
would get me the right results, but I'm not allowed to do that because this is a course assignment and it needs to be able to read different data sets as well.
4 件のコメント
madhan ravi
2019 年 4 月 18 日
" I am not allowed to do that " Why?
Fangjun Jiang
2019 年 4 月 18 日
You can specify range in xlsread(). See the help file.
Adam Danz
2019 年 4 月 18 日
As Fangjun Jiang mentioned, you need to use the xlRange input. Here's an example:
Customize that example to suit your needs. If you get stuck, be more specific about what the problem is.
Walter Roberson
2019 年 4 月 18 日
Note: if you do not happen to be on MS Windows with Excel installed, then xlsread() reads all of the file and then discards the parts outside of the range you specified.
採用された回答
その他の回答 (1 件)
Aylin
2019 年 4 月 18 日
Hi Rylan, as Fangjun and Adam have both commented, you can provide a range to xlsread.
However if you have R2019a, I would also recommend using readmatrix with the Range name-value pair to avoid writing Excel-style ranges (like "D3:D367"):
daily_maximum = readmatrix("datafile.xlsx", "Range", [3 4 367 4]);
If your column is at different positions in the file, but always has the same name, then it might be convenient to use readtable instead and index into the variable name. This way you wouldn't need to worry about changing your code when the column position changes:
data = readtable("datafile.xlsx");
daily_maximum = data.DailyMaximum;
I hope this helps!
Rylan
2 件のコメント
Adam Danz
2019 年 4 月 18 日
Note that you can also achieve flexible column indexing by reading in the headers and chosing the column index by using strcmp().
Rylan Thronburg
2019 年 4 月 18 日
編集済み: Rylan Thronburg
2019 年 4 月 18 日
カテゴリ
ヘルプ センター および File Exchange で Data Import from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!