Error: Index in position 1 exceeds array bounds.

조회 수: 4 (최근 30일)
Zuha Yousuf
Zuha Yousuf 2019년 8월 6일
댓글: Star Strider 2019년 8월 6일
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.

채택된 답변

Star Strider
Star Strider 2019년 8월 6일
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.
  댓글 수: 2
Zuha Yousuf
Zuha Yousuf 2019년 8월 6일
Thank you so much! That worked.
Star Strider
Star Strider 2019년 8월 6일
As always, my pleasure!

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

추가 답변 (1개)

Guillaume
Guillaume 2019년 8월 6일
편집: Guillaume 2019년 8월 6일
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
Zuha Yousuf
Zuha Yousuf 2019년 8월 6일
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))

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

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by