Plot using cell array text

조회 수: 1 (최근 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