Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting an existing MATLAB plot into another figure

Tags:

plot

matlab

I used the plot command to plot a figure and then changed lots of its properties using set command. I also store the handle of the plot (say h1).

What I need is to use the handle to plot the same figure again later in my code. I checked the plot command and did not find any version that accepts handle. I also thought of getting the Xdata and Ydata and use them to re-plot the same figure.

What is the simplest solution?

Edit 1: A working sample code based on copyobj that PeterM suggested.

hf(1) = figure(1);
plot(peaks);

hf(2) = figure(2);
plot(membrane);

hf(3) = figure(3);
ha(1) = subplot(1,2,1);
ha(2) = subplot(1,2,2);

for i = 1:2
    hc  = get(hf(i),'children');
    hgc = get(hc, 'children');
    copyobj(hgc,ha(i));
end

Edit 2: I also found this function that can copy figures (including legend) into a subplot.

like image 271
ManiAm Avatar asked Aug 28 '13 00:08

ManiAm


2 Answers

I have run into this situation before. Depending on what you are trying to do the function copyobj may be appropriate. This function lets you take the contents of one axes and copy it to a new figure.

like image 179
PeterM Avatar answered Nov 05 '22 19:11

PeterM


Improving @PeterM nice answer, one easier way would be:

fig2H=copy(gcf) % or change gcf to your figure handle

But it depends on what you want, if you want only the axes, or the whole figure… (btw, it doesn't seem to copy the legend handle).

like image 45
Werner Avatar answered Nov 05 '22 20:11

Werner