Index out of bounds
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Dee Tress
am 3 Mär. 2015
Kommentiert: Dee Tress
am 3 Mär. 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);
0 Kommentare
Akzeptierte Antwort
Andrew Newell
am 3 Mär. 2015
Bearbeitet: Andrew Newell
am 3 Mär. 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));
Weitere Antworten (1)
Joseph Cheng
am 3 Mär. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!