Passing strings into function i get 'too many input arguments'

14 次查看(过去 30 天)
I've written a function to calculate gpa. I would like to pass in any number of inputs into the function, for example gpa('a+', 'b-', 'a', 'c+').
The function works fine when I only input one letter grade, but any more than one and I get the error 'too many input arguments.'
I've duplicated the code and run it as a script with passing in the letter grades as part of the script and it works fine. Calling the function is when I have problems.
function grade_pt= gpa(grade_in)
%user inputs a string consisting of the letter grade including '+' or '-' %function returns the equivalent grade point
grade_in = {grade_in};
grades= {'a+','a', 'a-', 'b+', 'b', 'b-', 'c+', 'c', 'c-', 'd', 'f'};
eq_pt = {4, 4, 3.7, 3.3, 3, 2.7, 2.3, 2, 1.7, 1, 0};
res=zeros(length(grade_in),length(grades));
grdvec=zeros(1,length(grade_in));
for n = 1:length(grade_in);
res(n,:)= strcmpi(grade_in(n), grades);
grdvec(n)=eq_pt{res(n,:)==1};
grade_pt=sum(grdvec)/length(grade_in);
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by