How can I edit code so only bottom graph is displayed.

1 view (last 30 days)
I tried using histcount, but I cannot get it to work. This code prints like the screenshot. I only want the bottom graph to show. How can I accomplish this?
Thank you
hist_female_age = histogram(female(:,1));
hist_male_age = histogram(male(:,1),hist_female_age.BinEdges);
hist = [hist_male_age.Values;hist_female_age.Values];
bins1 = 45:10:95;
xlabel("Age of patient") ;
ylabel("Number of patients") ;
legend("Male Patient","Female patient") ;

Answers (2)

dpb on 6 Aug 2022
W/o the data and w/o knowing what didn't work, the simplest way to just get the second plot alone by itself would be to just draw it by itself -- since you're only using histogram as a crutch and don't care about the plot,
hist_female_age = histogram(female(:,1));
hist_male_age = histogram(male(:,1),hist_female_age.BinEdges);
ages = [hist_male_age.Values;hist_female_age.Values];
bins1 = 45:10:95;
xlabel("Age of patient")
ylabel("Number of patients")
legend("Male Patient","Female patient")
will leave you with just one full-sized figure...or you could leave as you have it and just create another figure at the end and replot bar into it...
Not sure where you went wrong with histcounts, but something like
x=mean([e(1:end-1);e(2:end)]); % bin midpoints
should create the bar graph more directly. (NB: Aircode, untested...)

Steven Lord
Steven Lord on 6 Aug 2022
The histcounts function does not return an object with properties like the histogram function does, but the outputs from histcounts serve the same purpose as some of the properties of the histogram object. The bin counts and the bin edges (which are the two properties you use from your first histogram call) are the first two outputs (respectively) from histcounts.
x = randn(1, 1e5);
[binValues, binEdges] = histcounts(x);
h = histogram(x, binEdges);
checkValues = isequal(h.Values, binValues)
checkValues = logical
checkEdges = isequal(h.BinEdges, binEdges)
checkEdges = logical

Community Treasure Hunt

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

Start Hunting!