how to plot a bar graph using for loop?
28 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have gotten my ouput results using a for loop. I have these results as :
Young aged positv test results: 7
Middle aged positive test results: 20
Old aged positive test results: 25
The x axis would be age groups, the y axis would be the # of positive results.
The code I used was here, but it displayed no bar graph:
%Read the data from positive test results
data.COVIDStatus(k)==1
%Include data which satisfies k condition for the appropriate age group
youngage_countx = 7
middleage_countx = 20
oldagecount_x = 25
%Create a bar graph which displays each x value
x = 7
x = 20
x = 25
%name y axis as positive test results
0 Commenti
Risposta accettata
Image Analyst
il 2 Mag 2023
OK, your first part is what I gave you in my answer : https://www.mathworks.com/matlabcentral/answers/1956714-invalid-use-of-operator-for-matlab-loop#answer_1227814
but you haven't accepted it yet. If my Answer solved your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
After that, see this:
youngage_countx = 7;
middleage_countx = 20;
oldagecount_x = 25;
values = [youngage_countx, middleage_countx, oldagecount_x];
% Create a bar graph which displays each x value
xLabels = {'Children & Young', 'Middle Age', 'Old Age'};
cats = categorical(xLabels);
bar(cats, values);
% Name y axis as positive test results
ylabel('Positive Test Results')
xlabel('Age of Patient')
grid on;
Più risposte (1)
dpb
il 2 Mag 2023
Modificato: dpb
il 3 Mag 2023
%Read the data from positive test results
%data.COVIDStatus(k)==1
%Include data which satisfies k condition for the appropriate age group
youngage_countx = 7;
middleage_countx = 20;
oldagecount_x = 25;
%Create a bar graph which displays each x value
%name y axis as positive test results
x = [youngage_countx; middleage_countx;oldagecount_x];
ages=categorical({'Young','Middle','Old'},{'Young','Middle','Old'},'ordinal',1);
hB=bar(ages,x);
ylabel('Positive Counts')
title('Covid Positive Counts by Age Group')
Avoid creating multiple variables with long names containing the metadata of some characteristic within the name to distinguish them; use arrays or cell arrays and corollary variable(s) with which to identify them.
2 Commenti
Image Analyst
il 2 Mag 2023
I tried ordinal=1 but without copying the category names in categorical and it sorted them alphabetically for some reason. That's why I had to add Children to my Young category, otherwise is, for some weird reason, sorted them and there was no option to not sort them that I could figure out.
dpb
il 3 Mag 2023
Modificato: dpb
il 3 Mag 2023
"I tried ordinal=1 but without copying the category names in categorical and it sorted them alphabetically for some reason."
Yeah, the categorical class isn't the most user-friendly thing, fer shure...specifying both the valueset and the catnames argument is the only way I could ever get 'ordinal' to actually have the desired effect. It'll say it's ordinal, and the tests of rank will work with the input order, but the display order always is alphabetical. I think it should qualify as a bug (or at least quality of implementation issue), but I don't recall whether I ever did submit a formal bug report or not.
If you provide the list in the desired order as the catnames second argument, that will convince it you meant what you said; it would seem to me that should be implied.
"That's why I had to add Children to my Young category, ...and there was no option to not sort them that I could figure out."
Only by actually using reordercats and redefining the variable will the the internal order be displayed externally--but it can be done.
c=categorical({'Fred','Barney','Wilma'},'ordinal',1); % create ordinal in nonalphabetic order
isordinal(c) % it is ordinal; it says so...
categories(c) % show names (sorted)
c(c=='Wilma')>c(c=='Fred') % test that hypothesis
c(c=='Wilma')==c(c=='Fred') % test that hypothesis
c=reordercats(c,{'Fred','Barney','Wilma'}); % put 'em in order intended...
categories(c)
Now have beat it into submission. Same thing happens on creation if use the other form...
c={'Fred','Barney','Wilma'};
c=categorical(c,c,'ordinal',1); % create ordinal in nonalphabetic order
categories(c)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!