Read a certain amount of rows from a column of an excel file

조회 수: 13 (최근 30일)
Rylan Thronburg
Rylan Thronburg 2019년 4월 18일
편집: Adam Danz 2019년 4월 22일
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
Adam Danz
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
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 4월 19일
편집: Adam Danz 2019년 4월 22일
Here are a couple options demonstrating how to work with the matrix you've already loaded into matlab. If your data is a cell array and not a matrix, some small adaptations will be needed.
This first section just creates some fake data and computes the number of segments, etc.
% Create data to work with: matrix
data = randi(100, 10000, 5);
startRow = 3; %starting row nuber
nRows = 365; %number of rows in segment
totalRows = size(data,1); %number of total rows
nSegments = floor((totalRows - startRow + 1) / nRows); %number of complete segments
nLeftover = totalRows -startRow + 1 -(nSegments * nRows); %number of rows at end that will be ignored
% Compute start and end indices of each segment
segStart = startRow : nRows : totalRows;
segStop = segStart(2:end) -1;
This section loops through each segment - use this if your project requires a for-loop that acts on each segment.
%Loop method
segment = cell(nSegements, 1);
for i = 1:nSegments
% This is the i_th section:
segment{i} = data(segStart(i) : segStop(i), :);
end
This section puts the segments into a 3-D array where segment 'n' is: segments(:,:,n)
segments = reshape(data(startRow:end-nLeftover,:), nRows, size(data,2), nSegments);
% Segement 'i' is segments(:,:,i)

추가 답변 (1개)

Aylin
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
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
Rylan Thronburg 2019년 4월 18일
편집: Rylan Thronburg 2019년 4월 18일
I just edited the question to be more clear, could you take another look? Additionally, if it's easier to read the data I need from a matlab matrix I can do that too, because I have already taken all the data out of excel.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by