Error: Index in position 2 exceeds array bounds

조회 수: 8 (최근 30일)
Zuha Yousuf
Zuha Yousuf 2019년 8월 14일
댓글: Neuropragmatist 2019년 8월 14일
I'm trying to import an xlsx file into MATLAB and make a simple graph of heart rate vs time. The time vector is from row 386 till 1645, and column 6 and the heart rate vector is from row 386 till 1645 and column 7. I get an error at the line HR = cellfun(@str2double, NumDataS(:,2)); where it says Index in position 2 exceeds array bounds. I have attached the .xlsx file here. Can anyone please help me in understanding where this error is coming from and how to fix it?
MATLAB CODE:
[~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','F386:G1645'); % Physiological Data
[~,TmatrixS] = xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','A386:F1645'); % Time Data
HR = cellfun(@str2double, NumDataS(:,2)); % Retrieve From Cell Array Of Strings
Time1=cellfun(@str2double, TmatrixS(:,5));%Array of minutes to be converted to seconds

채택된 답변

Star Strider
Star Strider 2019년 8월 14일
I recognised my code, so I went back and looked this up. (Reference: Error: Index in position 1 exceeds array bounds.)
Your Excel file does not encode everything in string variables this time, so a much more conventional approach will work.. Also, ‘NumData’ (actually ‘HR’) is a single column, not two, so your xlsread call for it must reflect that.
Try this:
[NumData]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','G386:G1645'); % Physiological Data
[Tmatrix] = xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','A386:F1645'); % Time Data
HR = NumData;
Time = datenum(Tmatrix); % Date Numbers
DTime = datetime(Tmatrix); % ‘datetime’ Array
figure
graph1=plot(Time, HR);
datetick('x', 'HH:MM:SS.FFF', 'keepticks')
figure
graph2=plot(DTime, HR);
This worked when I ran it.
  댓글 수: 2
Zuha Yousuf
Zuha Yousuf 2019년 8월 14일
Thank you so much! That worked. You've always been very helpful. :)
Star Strider
Star Strider 2019년 8월 14일
As always, my pleasure! Thank you!

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

추가 답변 (1개)

Neuropragmatist
Neuropragmatist 2019년 8월 14일
편집: Neuropragmatist 2019년 8월 14일
Your line fails because NumDataS is empty, because the 'text' field of xlsread is empty. I'm not really sure why but the third output of xlsread seems to contain the cell array you are expecting.
So your code will work if you just change to this line:
[~,~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','F386:G1645');
I'm guessing you will have to do the same for the other spreadsheet, but you will have to check:
[~,~,TmatrixS] = xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','A386:F1645');
Hope this helps,
M.
  댓글 수: 4
Star Strider
Star Strider 2019년 8월 14일
@Metioche — Your code is essentially correct (adapting code that I wrote earlier), and has a context of an earlier Question I Answered. The problem is that:
[~,~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','F386:G1645');
should be:
[~,~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','G386:G1645');
to read only the ‘HR’ column, not the ‘seconds’ column from the time array as well. The extra column is an error that causes problems with the rest of the code.
Neuropragmatist
Neuropragmatist 2019년 8월 14일
Ah I see now, I didn't check the HR = line, this could easily be fixed in my code by using:
HR = cell2mat(NumDataS(:,2));
Good that you caught it though,
M.

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

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by