i want to put this in order from smallest to largest population instead of alphabetical order. How can I do this?
clc
clear all
close all
%reading the file
[numbers text]=xlsread('homelesspopinamerica.xlsx');
%manipulating withthe data and storing it at our convenience
statenames=text(2:51,1:2);
pop=numbers(1:50,:);
states=statenames(:,2);
state=statenames(:,2);
homepop=pop(:,2);
homeperc=pop(:,4);
totpop=pop(:,3);
states=categorical(states);
%plotting
figure
bar(states,homepop)
xlabel('States', 'Fontsize', 18)
ylabel('Numer of Homeless Persons (Millions)', 'Fontsize', 18)
title('HOMELESSNESS IN AMERICA 2017', 'fontsize', 24);
set(gcf,'color','w');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%turn on grid lines
set(gca,'XGrid','off')
set(gca,'YGrid','on')

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Jun. 2018

1 Stimme

If you have a look at https://www.mathworks.com/help/matlab/ref/categorical.html then you can see that when you construct the categorical array, you can pass "valueset" as the second parameter, and that the categories would be ordered according to the values in valueset. So you could pass the populations of the states in the second parameter, and the corresponding state names in the third parameter, and that would create names sorted by population.

Weitere Antworten (1)

Sandro Lecci
Sandro Lecci am 7 Jun. 2018

0 Stimmen

Hi,
Use the sort function as following:
...
homepop = pop(:,2);
[~, I] = sort(homepop, 'ascend');
% remove/comment this line --> states = categorical(states);
...
bar(homepop(I));
set(gca, 'XTickLabel', states(I))
...
Best, Sandro

5 Kommentare

Minka Califf
Minka Califf am 7 Jun. 2018
Hi Sandro,
When I add that to my code it gives me an error code and it seems to mess with the title and labels on the x-axis.
Sandro Lecci
Sandro Lecci am 7 Jun. 2018
please post the updated code and the error message as well
clc
clear all
close all
%reading the file
[numbers text]=xlsread('homelesspopinamerica.xlsx');
%manipulating withthe data and storing it at our convenience
statenames=text(2:51,1:2);
pop=numbers(1:50,:);
states=statenames(:,2);
state=statenames(:,2);
homepop=pop(:,2);
homeperc=pop(:,4);
totpop=pop(:,3);
%states=categorical(states);
...
homepop = pop(:,2);
[~, I] = sort(homepop, 'ascend');
% remove/comment this line --> states = categorical(states);
...
bar(homepop(I));
set(gca, 'XTickLabel', states(I))
...
%plotting
figure
bar(states,homepop)
xlabel('States', 'Fontsize', 18)
ylabel('Numer of Homeless Persons (Millions)', 'Fontsize', 18)
title('HOMELESSNESS IN AMERICA 2017', 'fontsize', 24);
set(gcf,'color','w');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%turn on grid lines
set(gca,'XGrid','off')
set(gca,'YGrid','on')
Here is the code I am entering, and I am given this error :
Error in bargraph2 (line 30)
bar(states,homepop)
Walter Roberson
Walter Roberson am 7 Jun. 2018
Since states is no longer categorical, then at the time of your call to bar(states, homepop) then states is still a cell array of character vectors, which is not accepted.
I suggest you look at the answer I posted on how to get any sorting order you want for categorical.
Sandro Lecci
Sandro Lecci am 8 Jun. 2018
Minka, I am not surprised it gives an error.
In the code you simply copied-pasted my section without checking for redundancies. I thought it was somehow clear that you should have replaced the line with the bar function instead of plotting it once as I proposed and then plot it again in your old version. I also re-wrote some of your lines to help you understand where to apply the changes, but it seems you did not get it..
I am glad you found a way to solve your problem. Best

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by