Error: Index in position 1 exceeds array bounds.

2 views (last 30 days)
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 607 till 1947, and column 6 and the heart rate vector is from row 607 till 1947 and column 7. I get an error at the line HR=numData(607:1947,7); where it says Index in position 1 exceeds array bounds. Can anyone please help me in understanding where this error is coming from and how to fix it?
The code is given below:
clc
clear
close all
numData=xlsread('Pluto_5.30.19_Propofol_vitals_004.xlsx','Sheet2','F607:G1947')
dbstop if error
HR=numData(607:1947,7);
Time=numData(607:1947,6);
graph1=plot(HR,Time)
I'm attaching the Excel sheet below for reference.

Accepted Answer

Star Strider
Star Strider on 6 Aug 2019
Try this:
[~,NumDataS]=xlsread('Pluto_5.30.19_Propofol_vitals_004.xlsx','Sheet2','F607:G1947'); % Physiological Data
[~,TmatrixS] = xlsread('Pluto_5.30.19_Propofol_vitals_004.xlsx','Sheet2','A607:F1947'); % Time Data
HR = cellfun(@str2double, NumDataS(:,2)); % Retrieve From Cell Array Of Strings
Timemtx = cellfun(@str2double, TmatrixS); % Retrieve From Cell Array Of Strings
Time = datenum(Timemtx); % Date Numbers
DTime = datetime(Timemtx); % ‘datetime’ Array
figure
graph1=plot(Time, HR);
datetick('x', 'HH:MM:SS.FFF', 'keepticks')
figure
graph2=plot(DTime, HR);
It makes several improvements, and actually appears to plot your data correctly w.r.t. time.

More Answers (1)

Guillaume
Guillaume on 6 Aug 2019
Edited: Guillaume on 6 Aug 2019
You've told xlsread to import from row 607 to 1947, therefore, you'll get a (1947-607+1 = 1341) x 2 array, where row 1 corresponds to the original 607th row, row 2 the original 608th, ..., and row 1341 to the original 1947th row. Column 1 of the array in matlab is the original column 6, and column 2 is the original column 7.
You don't get a 1947x6 array where only rows 607 to 1947 and column 6 and 7 are filled.
So, simply:
numData=xlsread('Pluto_5.30.19_Propofol_vitals_004.xlsx','Sheet2','F607:G1947');
hline = plot(numData(:, 1), numData(:, 2))
and you're done
  1 Comment
Zuha Yousuf
Zuha Yousuf on 6 Aug 2019
Hi! When I do that, I get the following error:
Index in position 2 exceeds array bounds.
Error in Graphstuff (line 8)
hline = plot(numData(:, 1), numData(:, 2))

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!