Plot using cell array text

3 vues (au cours des 30 derniers jours)
Chriss
Chriss le 22 Avr 2017
Commenté : Chriss le 23 Avr 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?

Réponse acceptée

dpb
dpb le 22 Avr 2017
Modifié(e) : dpb le 23 Avr 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 commentaires
dpb
dpb le 23 Avr 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 le 23 Avr 2017
Thank you, looks like it works now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by