Get Figure for Axes in GUI from function

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

Thanks for the answer, but it is not what I have asked.
Why wouldn't I use a function instead of burying my figure into GUI? Anyway, this is not something I will discuss. I will do that in another function.
I have explained the thing what you did not understand. I have a function, a simple engineering code. 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.
So, I do not use plot command. When the user click on CALCULATE button on my GUI, code calls the function and a pop-up figure appears from the function itself. I want that figure to be on the axes.
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.
I am sorry, it is probably my fault that I cannot tell my problem correctly.
Your suggestion plot(axisHandle,x,y,...) will only work if I will plot the figure inside the GUI. I cannot share the code due to confidetality, I am trying to explain.
GUI Code
Gets input from user and calls BRA function using inputs.
textresults=BRA(inputs)
When this code is executed, due to aforementioned figure(...) code, a figure pops up. That's ok to pop up, I know I can set visibility to hide. That's not the question. I want that figure to be shown as a result to the user.
But user cannot see anything because figure does not shown in the axes.
BRA Code
The figure code I have shared before is the start of the figure. Later on I plot some data on the same figure which is not relavant to the question. The only part relavant to the question is the creation of the figure which I already shared.
What I need?
I need a part of code in GUI which gets the figure from BRA and show it to the user in axes1.
Your suggestion codes do not work since you want to plot inside the GUI.
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)
[textLabel, resultfigure]=BRA(tunnel_input, Name);
textLabel_t=textLabel(1:13);
copyobj(resultfigure,axes4)
resultfigure in workspace
Axes4
Results:
Undefined function or variable 'axes4'.
Error in GUI_BD_Volume_Loss>pushbutton1_Callback (line 150)
copyobj(resultfigure,axes4)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI_BD_Volume_Loss (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI_BD_Volume_Loss('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
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.
Resultfigure is generated in BRA using the following code and additional ones:
resultfigure=figure('Renderer', 'painters', 'Position', [50 100 1200 900])
resultfigure=figure('Renderer', 'painters', 'Position', [50 100 1200 900])
BRA function is called from calculate button's function.
Yes, I use GUIDE.
So, according to your statement, since it is a figure generated in BRA, I can't copy that to axes in GUI?
Rik
Rik on 22 Apr 2020
Edited: Rik 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.
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.
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 Interactive Control and Callbacks in Help Center and File Exchange

Tags

Asked:

on 22 Apr 2020

Edited:

on 23 Apr 2020

Community Treasure Hunt

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

Start Hunting!