MATLAB Answers

What is the best way to create a figure from an existing axes in a GUI?

1 view (last 30 days)
Alon Rozen
Alon Rozen on 20 Dec 2016
Edited: Jan on 26 Dec 2016
Hi all,
Using GUIDE I created a GUI and in it I placed an axes where I plot some graphs. Of course, the size of the plot area is fixed to the size of the axes I created while constructing the GUI. From time to time there is a need to plot the same data on a figure (outside the GUI) where I can change size or just keep it when I plot a different graph on the same axes on the GUI. I know that I can create a figure, recalculate whatever was calculated for the fixed graph and direct the plot to the new figure.
My question is: is there a simple way to direct the data in the existing GUI axes to a newly created 'fig' without re-calculating the data?


Sign in to comment.

Accepted Answer

Jan on 20 Dec 2016
Edited: Jan on 26 Dec 2016
This is a job for copyobj:
function YourGUI
FigH = figure('Title', 'The GUI');
AxesH = axes('Parent', FigH, ...
'ButtonDownFcn', @AxesCallback);
plot(1:10, rand(5,10), 'Parent', AxesH);
function AxesCallback(AxesH, EventData)
% [EDITED] Reject single left clicks:
DlgH = ancestor(AxesH, 'figure');
SelectionType = get(DlgH, 'SelectionType'); % Or use the EventData
if strcmpi(SelectionType, 'normal')
newFigHJ = figure;
newAxesH = copyobj(AxesH, newFigH); % [EDITED, Arguments swapped]
% [EDITED] Adjust position:
set(newAxesH, 'Units', 'normalized', 'Position', [0.1, 0.1, 0.8. 0.8]);
The UIContextMenu of the figure or of the axes, or a specific button under the axes would be a nice method to trigger the exprt also.


Show 2 older comments
Alon Rozen
Alon Rozen on 21 Dec 2016
Thanks Walter - it did the trick :)
Now I was left with two problems:
How to do it with double click instead of one click?
The maximum size of the copied graph is the size of the original graph. How do I copy in a way that when I maximize the new figure size the data will be stretched to fill the figure?
Alon Rozen
Alon Rozen on 21 Dec 2016
Thanks Jan :)
You solved it all!! The only remark I have is that the order of the variables inside 'copyobj' should be reversed.
Thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!