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

1 次查看(过去 30 天)
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!
clear
clc
i=1;
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);
i=i+1;
condition=true
while condition
next=input('Want to enter student info; ');
if next==0
for almostlength=1:i
currentlist=a{almostlength};
end
disp('end');
condition=false;
elseif next==1
studentname=input('Enter the student name: ','s');
semgpa=input('semester gpa?: ');
cgpa=input('cumulative gpa?: ');
almost = almost(studentname, semgpa, cgpa, i);
disp(almost)
end
end

回答(2 个)

Donald Thomas
Donald Thomas 2019-11-14
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;
else
almost = 0;%returns almost to starting value
end
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
else
repeat=i;%sets the value of "repeat" in order to repeat this function until i=repeat
i=1;
FakeDeansList{i}=FDL.studentname;
i=repeat;
almost=0;%returns almost to starting value
end
else
almost=0;%if i does not equal one the function will return almost to its starting value and the function will
end
end

Adam
Adam 2019-11-14
编辑:Adam 2019-11-14
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!

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by