MATLAB: Index out of bounds

indexvalue errorvariables

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);

Best Answer

  • 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));