MATLAB Answers

Error: Index in position 2 exceeds array bounds

33 ビュー (過去 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

  0 件のコメント

サインインしてコメントする。

採用された回答

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 件のコメント

表示 1 件の古いコメント
Neuropragmatist
Neuropragmatist 2019 年 8 月 14 日
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.
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by