필터 지우기
필터 지우기

Creating a function that returns multiple graphs

조회 수: 13 (최근 30일)
Snoopy
Snoopy 2018년 12월 27일
편집: TADA 2018년 12월 31일
I would like to create a function that returns as output arguments two different histograms such that in the script file either of the two graphs can be called. If I am not mistaken, I could do this for one histogram as follows:
function myplot(data,nbins)
histogram(data,nbins)
end
How do I adjust the code for two histograms?
  댓글 수: 2
Image Analyst
Image Analyst 2018년 12월 27일
편집: Image Analyst 2018년 12월 27일
What is x and what is y? Are they both data, or is y the number of bins or the edges for the histogram? Do you want just the counts like histcounts() returns? Or do you want the full histogram object with tons of information like histogram() returns?
Snoopy
Snoopy 2018년 12월 27일
I edited my question. I would like that the function returns what the histogram function returns. I even want to add a title, legend, etc. to each histogram.

댓글을 달려면 로그인하십시오.

답변 (1개)

TADA
TADA 2018년 12월 27일
function [h1,h2] = myplot(data, nbins)
h1 = histogram(data,nbins);
h2 = histogram(data,nbins);
end
% Or:
function h = myplot(data, nbins)
h(1) = histogram(data,nbins );
h(2) = histogram(data,nbins);
end
You Will Have To Decide How You Want To Send The Data To This Function. If In Matrices Or Cell Arrays Or Multiple Arguments (worst Option Probably), but This Above Is The General Idea.
The Second Option Is Somewhat More Versatile As You Can End Data An Array And Run In A Loop Generating As Many Histograms As You Need And Return All Of Them In A Vector Of Histogram Handles.
  댓글 수: 10
Snoopy
Snoopy 2018년 12월 31일
편집: Snoopy 2018년 12월 31일
data_sim = trnd(3,[3000,1]);
nbins = -5:0.01:5;
Histogram one:
histogram(data_sim,nbins,'FaceColor','white','EdgeAlpha',0.15);
title('Fig: a')
legend('a')
ylabel('a')
xlabel('a')
Histogram two:
histogram(data_sim,nbins,'FaceColor','white','EdgeAlpha',0.15);
line([t t],ylim,'Color','blue')
title('Fig: b')
legend('b','b')
ylabel('b')
xlabel('b')
So how do I create a function that returns either of the two histograms when I call one in the script file?
TADA
TADA 2018년 12월 31일
편집: TADA 2018년 12월 31일
I only see two options here, but they have different arguments
Histogram 1 only takes in 2 arguments (data_sim, nbins)
while Histogram 2 takes 4 arguments (data_sim, nbins, t, ylim)
If your third option has a different number of arguments you can use number of arguments to activate the different options of the function instead of the switch block:
h1 = doesItAll(rand(1,100), 10);
h2 = doesItAll(rand(1,100), 10, 0.5, 10);
function h = doesItAll(data_sim, nbins, t, ylim)
plotType = '';
if nargin >= 2
h(1) = histogram(data_sim,nbins,'FaceColor','white','EdgeAlpha',0.15);
plotType = 'a';
xAxeLbl = 'a';
yAxeLbl = 'a';
plotLegends = {'a'};
end
if nargin >= 4
h(2) = line([t t],[0 ylim],'Color','blue') ;
plotType = 'b';
xAxeLbl = 'b';
yAxeLbl = 'b';
plotLegends = {'b' 'b'};
end
title(['Fig: ' plotType]);
legend(plotLegends);
ylabel(yAxeLbl);
xlabel(xAxeLbl);
end
by the way, I changed the call to line function because there was an error or I sent the wrong value in ylim

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by