Plot using cell array text

2 次查看(过去 30 天)
Chriss
Chriss 2017-4-22
评论: Chriss 2017-4-23
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?

采纳的回答

dpb
dpb 2017-4-22
编辑:dpb 2017-4-23
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 个评论
dpb
dpb 2017-4-23
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 2017-4-23
Thank you, looks like it works now.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by