2
votes

I've encountered such a problem, and hope you guys could help me out here.

I have a plot in my GUI, contained multiple lines with different linspecs and a group of legends. And I've made a context menu which should allow users to open the EXACTLY same plot(retaining all line settings, title, legends, and so forth) in a new window(default figure, where it is able to save/edit the figure). However I couldn't find a simple way to migrate the plot, except re-run the plot commands which is quite complicated(plot different data, etc.)

So, I am looking for the solution in the following two ways:

  1. is there a simple way to migrate the plot into new figure window?

  2. or is it possible to save the plot directly with current interface?

For 2, I'd like to clarify that I only want to save the plot, not all GUI interface. I've tried saveas(handle.Plot,...) but it saved the GUI interface as an entity.

I hope the point has been made clear, thanks for your time. Cheers.

1

1 Answers

3
votes

For this task you can probably use the builtin Matlab function copyobj which does exactly this (i.e. the first option mentionned in your question).

The following piece of code demonstrates its usage:

h1=figure;
a1=plot((1:100),rand(1,100),'r-');
hold on
plot((1:100),rand(1,100),'b+');
legend({'plot1';'plot2'});
h2=figure;
copyobj(get(h1,'children'),h2);

Hope it works as well in your case.

UPDATE: as far as I understand this, your second solution would involve the saveas function which unfortunately works with the figure environment and not with axes (as you experienced it). So a workaround would probably involve copying the desired axes to a new figure with the method given above and then use saveas.