1

In Matlab I have a function calling a set of parameters. I have 3 sets, and I can call them individually and plot them.. No problems there. But I want to be able to compare them in one single plot. Meaning: is there a way, that I have get all three plots into a single plot?

function [GE,SI,RES,T] = glusim(parameter,Data) [...] If I call glusim(1,Data) I use a set of parameters, and gets a plot. If I call glusim(2,Data) I use another set of parameters and get a plot. If I call glusim(3,Data) I use a third set of parameters and get a plot. I want to compare all 3 in one plot.

How do I do that?

1 Answer 1

1

The command hold on allows you to plot in an existing axes without erasing what's already been plotted.

Alternately you can use hold all which behaves similarly but ensures that additional plot commands will cycle through a predefined color list (unless you overide this).

So a quick example of how you might do this is:

figure; % Create new figure window
hold on; % Retain graph when adding new ones
plot(Data1);
plot(Data2);
plot(Data3);

The slightly more involved answer is that you also need to ensure that all of your plot commands are plotting to the same figure. When you call the plot command, it plots to whatever figure is considered the "current figure". The explanation of this is a little advanced and involves storing and calling figure handles, but I've supplied a more detailed example below.

To get more control over what figure is the current figure, you might modify the above code as follows:

h = figure; % Create new figure window, assign figure handle to h
plot(Data1);

% ... Do things other than plotting

figure(h); % When ready to plot, set figure "h" to be the current figure
hold on; % Apply hold to ensure you won't erase previous plots
plot(Data2);

So to answer your original question, if the plotting is being done inside the function itself, you will need to ensure that the function has access to the handle of the figure you want to plot to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.