Using a histogram for cell array of letter grades

Hello,
I am trying to display the grade distribution showing frequency of grade by term of 4 total terms graphically. I am using a subplot to display all the data in one view.
Here is what I have:
% Subplot to show letter grade distribution
%by term
subplot(2,2,1)
hist(Fall2016_letter_grades)
title('Fall 2016 Grade Distribution')
xlabel('Letter Grade')
ylabel('Total')
subplot(2,2,2)
hist(Summer2016_letter_grades)
title('Summer 2016 Grade Distribution')
xlabel('Letter')
ylabel('Total')
subplot(2,2,3)
hist(Winter2016_letter_grades)
title('Winter 2016 Grade Distribution')
xlabel('Letter Grade')
ylabel('Total')
subplot(2,2,4)
hist(Fall2016_letter_grades)
title('Fall 2016 Grade Distribution')
xlabel('Letter Grade')
ylabel('Total')
When I try to execute the script I get this error
Error using hist (line 48) Input arguments must be numeric.
Error in grade_distribution_by_term (line 4) hist(Fall2016_letter_grades)
From the error message I can tell that the cell array of letter grades in this form Fall2016_letter_grades={'A', 'B', ....... etc} is causing the issue.
My attempts to convert the variable from str2num or str2double were unsuccessful

1 Comment

It is best practice not to open a new question when it is a follow up question (which itself was a follow up to an answered question). I'm glad to see that you tried my suggestion of using a histogram, but please refer back to the original questions, after following these two links: here and here.

Sign in to comment.

Answers (1)

Use the third output of unique to generate numeric values from the cell array of strings:
[uni,~,idx] = unique(Fall2016_letter_grades);
hist(idx,unique(idx))

2 Comments

Thank you. Now my x axis does not display the letter grades. I would like the x-axis to show the letter and the y-axis to display the total number.
If you look into the help, you will see that you can use a grouping variable to do this. Play around a bit to see what works.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Asked:

H G
on 29 Mar 2017

Commented:

Rik
on 29 Mar 2017

Community Treasure Hunt

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

Start Hunting!