Get Figure for Axes in GUI from function
Show older comments
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)
(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
on 22 Apr 2020
Adam Danz
on 22 Apr 2020
It's OK to create the figure in an external function but from your question, " 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.)" it appears as if your justification for using an external function is because it's 50 lines.
Why wouldn't I use a function instead of burying my figure into GUI?
If the plotting function is designed to be used outside of the GUI, then an external function is the way to go. But if the function is only used from within the GUI, there are benefits of using a local function within the GUI. GUI files can become large but since each function has its own workspace, the length of the file doesn't hinder processing power. Keeping the figure function external is fine, but a length of 50 lines doesn't prevent you from including it within the GUI if you wanted to do so.
"I have explained the thing what you did not understand."
The question was,
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?
1) you can't print a figure to an axes. An axes is a child to a figure. You can print an axes to a figure but not the other way around.
2) The reason your function produces a new figure (what you call a popup window), is because you are producing a new figure using figure(. . .) which I mentioned in my answer.
3) what does it mean, "I have tried to change the code above to aa=figure(....) and try to get the aa figure" ? If you're storing the handle 'aa', what's the problem of getting the aa figure?
4) You provided 2 lines of code and expect people to know what the problem is?
"In the end it creates a plot. When I call my function from the GUI, figure is shown as pop-up instead of showing in axes."
As I mentioned in my answer, that's because you are creating a new figure from within your external function.
"I do not use plot command." & "I want that figure to be on the axes."
I don't know what commands you're using becuase I can't see your code. You must be using some plotting functions (plot, scatter, controur, histrogram, imagesc, .....). Whatever you're using you must specify the axes so your plotted object appears in the correct axes. For almost all plotting function, the axis handles is specified as the first input.
plot(axisHandle, x, y, ...)
scatter(axisHandle, x, y, ...)
% etc...
% where axisHandle is the handle to your GUI axis
All of this is explained in my answer. If you still have trouble understaning it, I'd be happy to help you further.
Berk Demir
on 22 Apr 2020
Adam Danz
on 22 Apr 2020
There are two ways to show an object (such as a line) in the external figure and within the GUI axes.
One way is to plot the line (or whatever object you're plotting) in both axes. To do that, you could use my solution.
The other way is to copy the object from the external figure to your GUI axes. To do that you need 1) the handle to the plotted object and 2) the GUI axis handle.
% h is the handle to the plotted object
% for example, h = plot(...)
% axis 1 is the handle to the GUI axes.
% it may appear as app.UIAxes or handles.axes1, etc.
copyobj(h, axis1)
Berk Demir
on 22 Apr 2020
Adam Danz
on 22 Apr 2020
The first link isn't showing an image.
1) What is resultfigure? If it's a figure or axis handle, this will not work. You can't copy a figure or axes to another axes. If it's a handle to a plotted object just as resultfigure = plot(x,y), then it will work after you fix the 2nd problem. Could you show me the line of code that produces the resultfigure variable?
2) Where is the BRA function being called? The error indicates that "axes4" either doesn't exist or the workspace calling copyobj() doesn't have access to that variable. Is BRA() called from within a GUI callback function?
3) It looks like you're using GUIDE to produce the gui, please confirm.
Berk Demir
on 22 Apr 2020
Where a figure (or its content) is generated doesn't matter. You have a figure. An axes cannot contain a figure. I could contain a screenshot of that figure. Is that what you want?
Especially for more complex GUIs it is not a good idea to use GUIDE, see this thread for helpful advice and a discussion on this topic.
Berk Demir
on 22 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);
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!