Creating a function that returns multiple graphs

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 Comments

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?
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.

Sign in to comment.

Answers (1)

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 Comments

Snoopy
Snoopy on 27 Dec 2018
Edited: Snoopy on 27 Dec 2018
Thanks. One more question. In this setup, how can I give different titles, legends, etc. to different historgrams? Do I simply add these under h(1) = histogram(data,nbins) and under h(2) = histogram(data,nbins)? Or, to take it a step further, if I wanted to add a line to the first histograms, do I add it after h(1) = histogram(data,nbins)?
something like that:
data = rand(100,2)*100;
nbins = [10 10];
myplot(data, nbins);
function h = myplot(data, nbins)
for i = size(data,2):-1:1
figure(i);
h(i) = histogram(data(:,i), nbins(1,i));
title('blah');
legend('blah blah blah');
%...
end
end
that won't return the titles legends and all other elements not directly belonging to the histogram
Snoopy
Snoopy on 27 Dec 2018
Edited: Snoopy on 27 Dec 2018
Then is it better to create different functions for different histograms?
I Don't Know What You're Working So I Really Can't Tell, but The Reason Is Not Technical...
You Can Send More Data Back And Forth, You Can Put All The Display Information In A Struct:
options.title = 'something';
options.legends = {'hist1'};
% etc...
And Send This To Function Which Will Use This To Generate The Figure.
And Can Return A Struct With All UI Elements
h.fig = figure();
h.histogram = histogram(data,nbins);
h.title = title(opt.title);
% And So On...
Have The Function return ThIs Struct
Snoopy, what do you really want to do? You can do virtually anything.
If you want a function that always takes a pair of arrays and plots them side-by-side on one figure, you can do that. If you want a function that takes one array and plots it with the labels and titles you pass in, and the handle to a single axis that you pass in you can do that too. If you want to plot 4 histograms in one function, then you can do that too. If you don't even want functions and just want to do everything in your main script, you can also do that.
The thing is not do say "Can we do this and is this better?" but to say what you want in advance and develop code to do that. So, what do you want?
Snoopy
Snoopy on 31 Dec 2018
Edited: Snoopy on 31 Dec 2018
Right, I should been specific. What I want is the following. I want to create a function file that returns three different graphs when this function is called in a script file. In the script file I generate data using the rnd function, and supply the function with this data. The graphs differ from each other in various respects. The first graph is a simple histogram of the generated data. The histogram takes a title and legend. The second graph overlays two histograms and again has its own title and legend. The third graph again overlays two hitograms, but also adds a vertical line. At this moment I cretaed three functions which return these graphs if I can call them in a script file. But I want to put these function files together so that I have one function file that returns the graph I want in a script file. How can I do this?
I don't quite understand...
It may help if you upload the functions you already wrote
Do you mean something like that:
function uiStuff = doesItAll(data, options, type)
switch type
case 1
% plot one histogram
case 2
% plot 2 overlayed histograms
case 3
% plot 2 overlayed histograms and vertical line
end
end
?
you can also use strings instead of 1/2/3 in the switch which is more readable
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?
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

Sign in to comment.

Asked:

on 27 Dec 2018

Edited:

on 31 Dec 2018

Community Treasure Hunt

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

Start Hunting!