Clear Filters
Clear Filters

bar graph command for range values

2 views (last 30 days)
frankenberry
frankenberry on 10 Jun 2018
Edited: frankenberry on 26 Mar 2020
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.
Stephen23
Stephen23 on 26 Mar 2020
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
dpb
dpb on 13 Jun 2018
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.
dpb
dpb on 13 Jun 2018
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)

Community Treasure Hunt

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

Start Hunting!