Handling figures built for different parameters in Matlab

i couldn't find a simple answer to this thing im trying to do in Matlab. i'm gonna simplify this - say i have a function file that does some calculation for given parameter a:
[]=my_function(...,a)
...
calculations to obtain some functions y_1(x),y_2(x),... for a given a
...
figure(1);
plot(x,y_1);
title('y_1(x)');
xlabel('x');
ylabel('y_1');
set(legend(sprintf('y_1(x) for a=%g',a)),'interpreter','latex','Location','best');
figure(2);
plot(x,y_2);
title('y_2(x)');
xlabel('x');
ylabel('y_2');
set(legend(sprintf('y_2(x) for a=%g',a)),'interpreter','latex','Location','best');
...
i'm interested in plotting y_1(x) for different values of a (say 5 different values), all in a single figure (with a proper legend of course). same goes for y_2(x),y_3(x),...
whats the simplest way to do it?
i was thinking of making my_function return the plot/figure handle for each of the y_i's and then make a new script/function file that will do something like:
a=[0,1,3,5,7];
h_1=my_function(...,a(1));
h_2=my_function(...,a(2));
...
and somehow combine the figures from h_1 and h_2, but im not sure how to do it properly with the legend and everything.

 Accepted Answer

If by "all in a single figure" you actually mean a figure window, look at the subplot function.
If by "all in a single figure" you actually mean inside a single axes, look at the hold function.

5 Comments

i already know hold, but you can't simply use it because the plots i want combined are built for a different parameter a, so its a different run... so its alot more troublesome than simply using hold on.
if you try making a loop of some kind to run over the different values of the parameter and add hold on for each of the figures, then how would you also combine the legends ??
it will only present the legend of the final iteration.
Record the handles of the objects you want to legend(). Pass those handles as the first parameter of legend(). This could involve returning graphics object handles from your function.
can you please give an example for how exactly you do this?
if i have:
h1=figure(1)
...
legend(...)
h2=figure(2)
...
legend(...)
how do i combine the plots so that the legends will also be combined?
ph{1} = plot(....);
leg{1} = 'My First Legend';
ph{2} = plot(....);
leg{2} = 'My Second Legend';
legend(cell2mat(ph), leg)
thanks, i think i worked out the issue.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!