Question

I would like to reuse a figure I create in Matlab script.

fig1 = figure;
plot(...);
title(...);
% ...
% now I would like to plot fig1 again with a different title
% ...
% now I would like to plot fig1 again as a subplot in a 2x2 grid

How can I do that without code duplication?

Can I use the figure object? Or perhaps save the plot object somehow?

Was it helpful?

Solution

plot and friends all work on the current axes, so just put all of that code (not including figure) into a separate (sub)function, then call it after setting up a new figure/title/subplot.

If you can't do this for whatever reason, check out the example at the bottom of the page here.

OTHER TIPS

fig1 = figure;

p1=plot(...);

title('something');

% ...

% now I would like to plot fig1 again with a different title

title('something else'); % This will replace the old title with the new one 'something_else'.

% now I would like to plot fig1 again as a subplot in a 2x2 grid

delete(p1);

subplot(2,2,1);

p1=plot(...);

OR, you can just refresh your figure (without closing it and opening another one...) by typing:

clf reset

This will reset all figure properties, such as background color. Then, you can re-plot whatever you like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top