Plot using cell array text

How do i make a plot with text imported from an excel file called Places. eg.: Places: Place 1 Place 5 Place 10 Place 5 Place 1 Place 4 I then want to make a plot that shows the different "places" on the xlabel and the number of occurences on the ylabel. Do i have to convert the text so it says 1,5,10,5,1,4 etc. or is it possible to plot?

 Accepted Answer

dpb
dpb on 22 Apr 2017
Edited: dpb on 23 Apr 2017
No, pretty straightforward with categorical arrays...
[~,txt]=xlsread('places.xls');
cats={'Place 1';'Place 2';'Place 3'; ...; 'Place 10'}; % define categories/order
places=categorical(txt,cats); % convert to categories
n=countcats(places); % count each category
bar(n) % bar plot the counts vs cat
set(gca,'xticklabels',cats) % label by category
NB: the names must be spelled correctly/exactly including spacing this way to produce exact matches to the defined categories in the cats array. You can use
unique(txt)
to get a list but it will be sorted alphanumerically and so won't correspond to numeric order; that's what defining the array takes care of. It's possible to write logic to deal with it, of course; above is the simplified story.

5 Comments

I had to remove one of the "cats" but then it worked! I've used the excel file earlier in the m-file - Do you know if i can refer to the first import (made global) or i have to import it again as not to mess with the rest. I've tried to just write "[~,txt]=places" but then it says i have too many input arguments?
dpb
dpb on 23 Apr 2017
Edited: dpb on 23 Apr 2017
Read the file only once and PASS the data, do NOT use GLOBAL, "go directly to jail, do not collect $200". Just replace txt with whatever variable you used to read the text portion of the spreadsheet; there's nothing magic in variable names, they're only identifiers.
"...but then it worked!"
You seem surprised. Why wouldn't it? :)
The file is excel imported as Table and the column 'Places' is automatical saved as 'Categorical'. Is this what causes it to write "Too many output arguments"? that i try to convert cat into cat?
Can't see your terminal from here, sorry, and the crystal ball is out of service (yet again! :( ).
Need to post enough code with the error in context so can see what you're trying to do...
But the syntax [~,variable]= is only valid as the LHS of an assignment from a function that has at least as many outputs produced as places (including those being ignored by the tilde placeholder) in the vector (two, here). The variable places in my solution is a single categorical variable array and so there is only one "output" to be assigned.
What you should do is use YOUR variable that contains the data wanted in the algorithm I've shown--if it's a table then it would be something like
tab=readtable('places.xls');
n=countcats(tab.places);
Use whatever you want to use for the table name where I used tab and whatever the returned variable name is for "places"
Read the doc on using |table|, particularly the section on accessing data therefrom...
Thank you, looks like it works now.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Apr 2017

Commented:

on 23 Apr 2017

Community Treasure Hunt

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

Start Hunting!