Index exceeds matrix dimensions error

조회 수: 13 (최근 30일)
Justin Howard
Justin Howard 2018년 4월 10일
답변: dpb 2018년 4월 10일
I'm trying to get my for loop to display the names that have systolic blood pressure over 90, but i get an error on the line with my fprintf statement saying my index exceeds matrix dimensions. im not sure what else to do?
data=textscan(fid,'%s%s%d%d','delimiter', '/');
Names=data(:,2);
Systolic=data(:,3);
Diastolic=data(:,4);
n=length(Systolic);
for i=1:length(n)
if (Systolic{i} > 90)
fprintf('%s is ideal',Names{:,2});
end
end
  댓글 수: 2
David Fletcher
David Fletcher 2018년 4월 10일
look at this on the second line
Names=data(:,2)
Now look at this in your fprintf statement
fprintf('%s is ideal',Names{:,2});
Do you see the problem?
Justin Howard
Justin Howard 2018년 4월 10일
yes but even when i change it, it still pulls up the same error.

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

채택된 답변

dpb
dpb 2018년 4월 10일
In Matlab, no loops needed--
data=textscan(fid,'%s%s%d%d','delimiter', '/');
Names=string(data(:,2)); % convert to string instead of cell array
Systolic=data{:,3}; % and to double array
Diastolic=data{:,4};
SystThresh=90; % don't bury "magic numbers in code; make variables
isOK=(Systolic > SystThresh) % logical addressing array of condition
fprintf('%s is ideal\n',Names(isOK)); % use the vector, Luke!
I'd suggest also look at readtable in lieu of textscan to get data as a table; much advantage there in processing by a variable name and in lookups for classifying by various variables, conditions...

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by