Error: Index in position 2 exceeds array bounds

1 次查看(过去 30 天)
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.

更多回答(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 中查找有关 MATLAB Functions in Microsoft Excel 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by