Clear Filters
Clear Filters

Get Figure for Axes in GUI from function

8 views (last 30 days)
Berk Demir
Berk Demir on 22 Apr 2020
Edited: Adam Danz on 23 Apr 2020
Hello,
I have created a simple GUI which gets inputs from user and bring them to a outside function (lets say BRA). This BRA function calculates some numerical outputs and give them as output 'results'.
In this BRA code, I have also created a figure which starts with following and worked on over 50 lines. (So I cannot do that in GUİ, I have to create the figure in the function.)
figure('Renderer', 'painters', 'Position', [50 100 1200 900])
subplot(2,1,1)
%etc. etc.
So my question is: I have created an axes with a tag name axes1, however, I couldn't find a way to print this figure to this axes. My function gives the figure as POPUP window. I have tried to change the code above to aa=figure(....) and try to get the aa figure, but I couldn't manage it.
Can you please help me to get the figure from my outside function and print it on GUI axes?

Answers (1)

Adam Danz
Adam Danz on 22 Apr 2020
Edited: Adam Danz on 22 Apr 2020
(So I cannot do that in GUİ, I have to create the figure in the function.)
Why? Because it's 50 lines? That doesn't prevent you from creating the figure within the GUI.
I couldn't find a way to print this figure to this axes.
In the 2 lines of code provided, I see you're creating a new figure and creating a subplot. What exactly do you want to print on the GUI axes? One of the subplots?
To plot directly to an axes, use the axes handle,
% Example
plot(handles.axes1, x, y, '-b')
  10 Comments
Berk Demir
Berk Demir on 22 Apr 2020
I didn't know that. Of course in BRA, I use the first figure command and then I use subplot and plot commands. But you say that I cannot put figure into the axes.
Thanks then.
Adam Danz
Adam Danz on 22 Apr 2020
Edited: Adam Danz on 23 Apr 2020
Berk Demir, here's what's wrong and here's a solution.
What's wrong
A figure can contain axes. The figure is the parent and the axes is the child.
Axes can contain objects such as lines and scatter points. The axes is the parent and the line objects are children.
To illustrate further, a library can contain books but you can't put a library in a book. Similarly, you cannot put a figure in an axes.
You can copy the children from one axes to another axes.
Solution
If you can't directly plot the objects to your GUI axes using the method in my answer (which is the best solution) you can create the external figure and copy the content of it's axes to your GUI's axes.
% figHandle is the handle to the external figure
axesHandle = findobj(figHandle,'type','axes');
% Copy all children of the external axis to your GUI axes.
% If there are more then 1 axes found on the figure, this
% will only copy the first axes listed.
% Replace the 2nd input with your GUI axes handle.
copyobj(axesHandle(1).Children, axisHandle);

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!