(Index in position 1 exceeds array bounds (must not exceed 1). error is popping up where it does not seem to apply

1 view (last 30 days)
Hey, this probably has a simple answer but I am in introductory CSCI and do not know the answer to it. My code on line 12 keep spitting out the index must not exceed array bounds error. It is supposed to call another function but it isn't doing that. I'd appreciate any help you guys could give. The problem ask us to create a function and a call function that creates a list of students who have a gpa between 2.9 and 3.0. I tested my function using my friends call function and it worked, so my call function seems to be the problem. Thank you so much!
studentname=input('input student name: ','s');
semgpa=input('input gpa for semester: ');
cgpa=input('input cumulative gpa: ');
almost=almost(studentname, semgpa, cgpa, i);
a{i}=almost(studentname, semgpa, cgpa, i);
while condition
next=input('Want to enter student info; ');
if next==0
for almostlength=1:i
elseif next==1
studentname=input('Enter the student name: ','s');
semgpa=input('semester gpa?: ');
cgpa=input('cumulative gpa?: ');
almost = almost(studentname, semgpa, cgpa, i);

Answers (2)

Donald Thomas
Donald Thomas on 14 Nov 2019
This is the function it is calling by the way
function FakeDeansList = almost (studentname, semgpa, cgpa, i)
almost = 0;%sets value for almost
FakeDeansList = {};
if semgpa >= 2.9 && semgpa < 3
almost = almost +1;
almost = 0;%returns almost to starting value
FDL.studentname = studentname;%these commands put the inputs for each respective section into the structure
FDL.semgpa = semgpa;
FDL.cgpa = cgpa;
FDL.almost= almost;
if FDL. almost == 1
if i==1
FakeDeansList{i}= FDL. studentname;
i= i+1;
almost = 0;%returns almost to starting value
repeat=i;%sets the value of "repeat" in order to repeat this function until i=repeat
almost=0;%returns almost to starting value
almost=0;%if i does not equal one the function will return almost to its starting value and the function will

Adam on 14 Nov 2019
Edited: Adam on 14 Nov 2019
Well, I don't know about line 12, which (from counting - please indicate which is the line of the error message in future, it would help rather than us having to count and assume this is the whole file!!) appears to be this one:
next=input('Want to enter student info; ');
but looking further up you have these lines:
almost=almost(studentname, semgpa, cgpa, i);
a{i}=almost(studentname, semgpa, cgpa, i);
So you call a function, then assign its output to a variable of the same name, which then hides the function for the next line.
Never name a variable the same as any function as it will simply hide the function. Matlab is a bit simplistic in that sense and will happily allow you to just hide function names with variable names. What it will now do on that next line is see almost as a variable, with whatever the result was of the first call to almost, the function.
So it will try to index into this variable using studentname. If the variable is a scalar then the only valid index is 1 and anything that evaluates to greater than 1 will throw this error. It is unfortunate too in Matlab that indexing into a variable is syntactically identical to calling a function with an argument, which makes this type of mistake all the more common amongst unsuspecting Matlab users, because the error message often throws them off too.
If that isn't line 12 then I don't know what is happening - firstly how it gets past this line making any sense of it and secondly how it could possibly throw that error in the line that appears to be line 12!
  1 Comment
Donald Thomas
Donald Thomas on 14 Nov 2019
That fixed it actually! That and some other small fixes but that was the main issue. Thank you for your response I really appreciate it.

Sign in to comment.


Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!