bar graph command for range values

sdad

2 Comments

dpb
dpb on 11 Jun 2018
Edited: dpb on 11 Jun 2018
Image wasn't attached...but, need to know which data are where in the array to collect how want. bar generates a bar for each column so apparently
size(ampsCAP) returns 4,8?
W/O knowing just where is what, can't provide exact code but look at the examples of how to use the various options in bar in the documentation including being able to specify a x-variable to group data.
Original question:
I have a program written by my supervisor that outputs a bar graph (latest version of Matlab on Windows 10-64 bit) for each variable (ampsCAP) as follows:
if numtests > 1
figure1 = figure('Color',[1 1 1]);
subplot(141); bar(ampsCAP(1,:)); title([int2str(Cs(1,1)) ' Hz']);
subplot(142); bar(ampsCAP(2,:)); title([int2str(Cs(2,1)) ' Hz']);
subplot(143); bar(ampsCAP(3,:)); title([int2str(Cs(3,1)) ' Hz']);
subplot(144); bar(ampsCAP(4,:)); title([int2str(Cs(4,1)) ' Hz']);
end
This outputs 4 sets of frequencies (500, 1000, 2000, 3000). Each frequency has 8 bars (each bar represents the amplitude of the modulator) for two conditions (normal, noise-exposed) with two levels (100% modulation, 50% modulation). For example, in the figure for 500 Hz:
#1 is normal at 100% modulation for the first modulator
#2 is noise-exposed at 100% modulation for the first modulator
#3 is normal at 50% modulation for the first modulator
#4 is noise-exposed at 50% modulation for the first modulator
I need to output this differently so that I have only two graphs with four frequencies (500, 1000, 2000, 4000) in each graph for the two conditions(normal, noise-exposed) in one graph (i.e.,100% modulation depth) and four frequencies for two conditions in the other graph (i.e, 50% modulation depth) in the other graph. I don't know how to re-write the above code to let me do that. I have attached an image of what the above code outputs.

Sign in to comment.

 Accepted Answer

dpb
dpb on 11 Jun 2018
Edited: dpb on 12 Jun 2018
if numtests > 1
figure1 = figure('Color',[1 1 1]);
subplot(141); bar(ampsCAP(1,:)); title([int2str(Cs(1,1)) ' Hz']);
subplot(142); bar(ampsCAP(2,:)); title([int2str(Cs(2,1)) ' Hz']);
subplot(143); bar(ampsCAP(3,:)); title([int2str(Cs(3,1)) ' Hz']);
subplot(144); bar(ampsCAP(4,:)); title([int2str(Cs(4,1)) ' Hz']);
end
could be written more succinctly as
if numtests > 1
figure1 = figure('Color',[1 1 1]);
for i=1:4
subplot(1,4,i); bar(ampsCAP(i,:)); title([int2str(Cs(i,1)) ' Hz']);
end
end
On the follow-up and towards the question--what happened to the eight values per frequency?
To separate by modulation value, use subarray indexing to pick the columns wanted for each--if there are just the four conditions outlined above, then
for i=1:4
subplot(1,4,i);
bar(reshape(ampsCAP(i,:),2,[]).','grouped');
set(gca,'XTickLabel',{'No noise';'Noise'})
title([int2str(Cs(i,1)) ' Hz']);
end
end
ADDENDUM
Had to dash earlier; give the following a go...again assumes there's a 4x4 array by frequency by row, the four categories outlined above by column...
x=categorical([1 2],[1 2],{'Quiet';'Noisy'}); % grouping variable
m=[100 50]; % modulation values
for i=1:2
subplot(1,2,i);
i1=2*i-1;
bar(x,[ampsCAP(:,i1:i1+1).','grouped');
legend(num2str(Cs(:,1), '%d Hz');
title(num2str(m(i),'%d% Modulation')
end
CAVEAT Air code, untested... :)

6 Comments

This still outputs 4 graphs instead of 2. I'm trying to get one graph to be 100% modulation with all 4 frequencies for No Noise and Noise and the other graph to be 50% modulation for No Noise and Noise. In my graphs above, the 100% modulations are the first two bars for each frequency. The 50% modulations are the second two bars.
dpb
dpb on 12 Jun 2018
Edited: dpb on 12 Jun 2018
Well, I couldn't really decide just which you meant...took best guess! :)
You have to arrange the data into the proper subsets; give it a go...to put all frequencies on one and to have only two plots means you need two calls to bar with the data oriented with frequency by column instead of by row...then per the doc for bar, groups are by row. Give it a go; reshape() and array subscripting are your friends... :)
It was a pretty good guess except I had written "I need to output this differently so that I have only two graphs with four frequencies (500, 1000, 2000, 4000) in each graph for the two conditions(normal, noise-exposed) in one graph (i.e.,100% modulation depth) and four frequencies for two conditions in the other graph (i.e, 50% modulation depth) in the other graph. This would work out to two bars for each frequency (8 bars in total) in each of the two graphs (100% and 50% modulation). I like the colours though. That's a nice touch. I'm still working on how to output the results so that I can get this.
if numtests > 1
figure1 = figure('Color',[1 1 1]);
title({'Response amplitudes of four modulators for normal and noise-exposed groups'}, 'FontName', 'Arial', 'FontSize',14);
subplot(121); bar(ampsCAP(1:4,1:2)'); xlabel({'100% modulation'}, 'FontName', 'Arial', 'FontSize',12);ylim([0 40]);
set(subplot(121),'FontName','Arial','FontSize',12,'XTick',[1 2],'XTickLabel',{'Normal','Noise-exposed'});
subplot(122); bar(ampsCAP(1:4,3:4)'); xlabel({'50% modulation'}, 'FontName', 'Arial', 'FontSize',12);ylim([0 40]);
set(subplot(122),'FontName','Arial','FontSize',12,'XTick',[1 2],'XTickLabel',{'Normal','Noise-exposed'});
% subplot(143); bar(ampsCAP(3,:)); xlabel([int2str(Cs(3,1)) ' Hz'], 'FontName', 'Arial', 'FontSize',12);ylim([0 40]);
% subplot(144); bar(ampsCAP(4,:)); xlabel([int2str(Cs(4,1)) ' Hz'], 'FontName', 'Arial', 'FontSize',12);ylim([0 40]);
end
Now I just need to make a bar legend but I'm having trouble with this. This is what I have but it overwrites my subplot (probably because of the legend command but I'm not sure how to fix this). Any suggestions:
y = bar(ampsCAP(1:4)');
set(y(1:1),'DisplayName','500 Hz');
set(y(2:1),'DisplayName','1000 Hz');
set(y(3:1),'DisplayName','2000 Hz');
set(y(4:1),'DisplayName','4000 Hz');
legend1 = legend(subplot(122),'show');
set(legend,'Position',[0.48 0.79 0.07 0.13]);...
'FontSize',12,...
'EdgeColor','none';
The image now looks like this:
Did you not see the updated Answer???
What, specifically was wrong in the result in the Answer? The legend should work automagically with the frequency values you have; I don't have the actual data to know precisely how it's formatted to be able to ensure reference correct.
Oh, on the legend; it just came to me what it was you were saying; I totally whiffed first go-'round.
What you want to do is to just look at the property names/values it generates to see what it is that you want modified, then pick those pieces out of the (generally terribly ugly/inefficient since it's machine generated) stuff it gives and use that to augment your script. There's no need to use the hardcoded label strings or handles once you see what properties you want set and to what values, go back to the original code and make the changes therein.

Sign in to comment.

More Answers (0)

Asked:

on 10 Jun 2018

Edited:

on 26 Mar 2020

Community Treasure Hunt

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

Start Hunting!