Input cell to function

5 views (last 30 days)
Stephen
Stephen on 4 Apr 2014
Commented: Jan on 4 Apr 2014
I have written a function which accepts as an input a cell array consisting of any number of strings. I have it structured as follows:
function grade_pt= gpa(varargin)
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(nargin,length(grades));
grdvec=zeros(1,nargin)
for n = 1:nargin;
res(n,:)= strcmpi(varargin{n}, grades);
grdvec(n)=eq_pt{res(n,:)==1};
grade_pt=sum(grdvec)/nargin;
end
end
I call this function like this:
gpa('a+','b-','b')
calling 'whos' within the function reveals (as I designed it) that I have input 3 values in a 1x3 cell.
When I call the function as I did above, or with any number of input arguments, everything works fine. The function computes the gpa and returns a double value. It does this by comparing each grade with the vector of grades, finding a logical match, and the matching grade_pt. It then sums the results and divides by the number of inputs to get the gpa.
I also made a structure consisting of students' names, their courses and the grades that they earned in each course.
I extracted the letter grades and stored them in a cell array called 'grades' where each row corresponds to a student. Here's an excerpt from the code, it has just the last 'student' and the code to create the cell array, 'grades.'
student(7).name = 'Tobias Funke';
student(7).courses(1).course_name = 'PSYCH 620';
student(7).courses(1).grade = 'A';
name = cell(1,7);
grades =cell(7,6);
for n=1:7
name{n} = student(n).name
for x = 1:length(student(n).courses)
grades{n,x} = student(n).courses(x).grade
end end
I then called the function gpa(grades(n,:)).
This returns an error:
"Error using strcmpi Inputs must be the same size or either one can be a scalar.
Error in gpa (line 16) res(n,:)= strcmpi(varargin{n}, grades);"
calling 'whos' reveals (much to my chagrin) that I have input only ONE value. That value is a cell array.
My question:
How do I take the contents of the cell array 'grades' break it up so that I can input it into my function and it will read it as 3 values.
Or, how do I alter my function to break up the input into its constituent parts (making sure though that a 'b+' is still read as a single value)??
HELP!!!
Thanks for reading all of this, I know its long. Id really appreciate any input or advice. Am I making this too difficult? Is there a much simpler solution?
  1 Comment
Jan
Jan on 4 Apr 2014
Please mark your code and press the "{} Code" button to improve the readability.

Sign in to comment.

Answers (2)

Andrei Bobrov
Andrei Bobrov on 4 Apr 2014
Edited: Andrei Bobrov on 4 Apr 2014
use
gpa(grades{n,:})
OR use new function gpanew.m :
function grade_pt= gpanew(incell)
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};
grade_pt = mean(cell2mat(eq_pt(ismember(grades,lower(incell)))));
end
here incell - cell array -> gpanew({'B-','c+'}) as your gpa('B-','c+') then use
gpanew(grades(n,:))

Stephen
Stephen on 4 Apr 2014
Nope. Doesn't work. That reads grades{n,:} into gpa as a single value. So, in the function, nargin = 1, when it should = between 4 and 6 depending on the student, or how many letter grades are stored in the cell.
How I got around it is
grades(cellfun('isempty',grades))=[];
then counted through to pull out the right letter grades and input based on indexing the new 'grades.'
  1 Comment
Jan
Jan on 4 Apr 2014
Please post comments in the comment section and not as an answer. Thanks.

Sign in to comment.

Categories

Find more on Variables 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!