1
$\begingroup$

I couldn't find a simple answer to this thing I'm trying to do in Matlab. I have a function file that does some calculation for a given parameter a:

[]=my_function(...,a)
...
Calculations to obtain some functions y_1(x),y_2(x),... for a given parameter 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). Likewise the same for y_2(x),y_3(x), etc....

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 I'm not sure how to do it properly with the legend and everything.

$\endgroup$
2
$\begingroup$

The most idiomatic MATLAB way to do this would be to store the different y results in an array and then plot the results with a single plot call.

For example:

a = 1:5;
x = linspace(0,2*pi);
y = sin(bsxfun(@times, a.', x));
figure
plot(x,y)

which is equivalent to

x = linspace(0,2*pi);
for a = 1:5
    y(ii,:) = sin(a*x);
end
figure
plot(x,y)

Alternatively, you can plot multiple things on the same axes by calling the hold function. For example:

x = linspace(0,2*pi);
figure
hold on
for a = 1:5
    y = sin(a*x);
    plot(x,y)
end

Edit:
You can make your legend as follows:

a = 1:5;
x = linspace(0,2*pi);

figure
hold on
for ii = 1:numel(a)
    y(i,:) = sin(a*x);
    legendStrs{ii} = sprintf('y_%d(x) for a=%g', ii, a(ii));
end
h = plot(x,y);
legend(h,legendStrs)
$\endgroup$
  • $\begingroup$ well, this is oversimplication of the problem i presented, but even here a problem arises if you want a legend to indicate the difference between the results of different parameters. how would you make the legend also "hold on" ? because when i tried it failed and it only presented the legend for the last plot $\endgroup$ – TensoR Apr 4 '16 at 20:31
  • $\begingroup$ @TensoR Did you look at the documentation for legend? It's really quite good: help legend. In short: legend('label 1', 'label 2', 'label 3', ...) or h=plot(x,y); legend(h,'label 1','label 2',...) where h is the vector of plot handles. $\endgroup$ – Doug Lipinski Apr 4 '16 at 20:37
  • $\begingroup$ What do you mean this is an oversimplification? If it does not answer your question in some way please clarify your question. $\endgroup$ – Doug Lipinski Apr 4 '16 at 20:39
  • $\begingroup$ yes you can put the legend by hand, but what if you wanted to build up the plot legend at each iteration? how do you make it work then? the "hold on" won't apply to the legend of each plot and you end up with the legend only for the last plot... inside the legend i put something like a=%g (as described in the question) $\endgroup$ – TensoR Apr 4 '16 at 21:06
  • 1
    $\begingroup$ See my update to the answer. Put the strings that make up the legend in a cell array, then use that and the curve handles to make the legend. That's in the documentation for legend. There are other ways that would work too (also found in the documentation). A large part of programming is reading the documentation and figuring out how to make the options work for you. Matlab has excellent documentation built in and it's almost always worth reading carefully to figure out if you can do what you want in the context of what's described there. $\endgroup$ – Doug Lipinski Apr 4 '16 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.