Index out of bounds

1 次查看(过去 30 天)
Dee Tress
Dee Tress 2015-3-3
评论: Dee Tress 2015-3-3
I am trying to use the patient number inputed into the filename to access the correct body information. There are 4 patients and each has 8 related properties. When I define which patient number it is from the filename, it brings up the correct number, and when I use that number manually to define the index in tbw the code works fine. But when I use the variable name, it somehow uses a different value for 'patientno' (its actually adding 48 to the value. e.g. patient 2 tries to index 50)
Here is my code:
patientno = filename(2);
patientstats = zeros(4,8);
patientstats(1:4,1:8) = [860.0,75.7,39.1,9.2,174.0,43.3,38.1,30.0; 702.0,62.2,36.5,9.9,165.0,39.3,40.0,29.0; 800.0,98.4,43.0,7.2,170.0,47.5,40.9,26.0; 980.0,80.2,53.9,12.3,175.0,41.0,41.0,27.5];
%Define individual anthropometric information
tbw = patientstats(patientno,1)
tw = patientstats(patientno,2);

采纳的回答

Andrew Newell
Andrew Newell 2015-3-3
编辑:Andrew Newell 2015-3-3
The variable name filename is probably char, not numeric. Suppose you define
filename = '12';
Then if you view the second character in the name, you see:
filename(2)
ans =
2
But if you try to use it as an index, it uses the ASCII number of the character, and that is:
double(filename(2))
ans =
50
So what you need to do is define patientno using
patientno = str2num(filename(2));

更多回答(1 个)

Joseph Cheng
Joseph Cheng 2015-3-3
From what i see filename is probably a string, so what you're doing is trying to access patientstats('2',1) and patientstats('2',2), which '2' is an ascii character which has the decimal equivalent of 50. you'll need to use str2num(patientno) to convert it to decimal.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by