简体   繁体   中英

How can reuse figure in Matlab?

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?

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM