Plot using cell array text

2 views (last 30 days)
Chriss
Chriss on 22 Apr 2017
Commented: Chriss on 23 Apr 2017
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
dpb
dpb on 23 Apr 2017
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...
Chriss
Chriss on 23 Apr 2017
Thank you, looks like it works now.

Sign in to comment.

More Answers (0)

Categories

Find more on Text Data Preparation 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!