Index out of bounds

1 view (last 30 days)
Dee Tress
Dee Tress on 3 Mar 2015
Commented: Dee Tress on 3 Mar 2015
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);

Accepted Answer

Andrew Newell
Andrew Newell on 3 Mar 2015
Edited: Andrew Newell on 3 Mar 2015
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 Comment
Dee Tress
Dee Tress on 3 Mar 2015
Thank you, this worked perfectly

Sign in to comment.

More Answers (1)

Joseph Cheng
Joseph Cheng on 3 Mar 2015
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.

Community Treasure Hunt

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

Start Hunting!