MATLAB Answers

0

Error: Index in position 2 exceeds array bounds

Asked by Zuha Yousuf on 14 Aug 2019
Latest activity Commented on by Neuropragmatist on 14 Aug 2019
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

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 14 Aug 2019
 Accepted Answer

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 Comments

Thank you so much! That worked. You've always been very helpful. :)
As always, my pleasure! Thank you!

Sign in to comment.


Answer by Neuropragmatist on 14 Aug 2019
Edited by Neuropragmatist on 14 Aug 2019

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 Comments

Show 1 older comment
Hmmm, that's strange, for me Matlab gives a cell array for the third output (raw data) which is a 1260x2 cell array with the data from the range you specified, which seems to be exactly what your later code expects. I'm not sure why the different result so you should stick to Star Strider's answer.
@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.
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.

Sign in to comment.