Plotting external function in App GUI graph

I've got a function which plots a simple sine wave called testgraph.m
I'd like to plot this function when a button is pressed in the app designer GUI i've made. I've managed to call up testgraph.m when the button is pressed (generating the plot in a separate figure window) but i'm struggling to find a way to link the UI axes graph within the GUI to the plotted graph by the function in my .m file.
I'd prefer not to include the code from the function itself in the app designer script as i'd like to keep it as clean as possible and just call in the external functions as needed.
Would anyone be able to point me in the right direction to solve this please?

 Respuesta aceptada

Adam Danz
Adam Danz el 26 de Nov. de 2019
Editada: Adam Danz el 22 de Abr. de 2020
Pass the axis handle into your testgraph() function so your external plotting function knows what axes to plot on.
It would look something like this
function ButtonPressFunction(app, event)
testgraph(app.UIAxes, . . .)
end
and in your external function,
function testgraph(h, . . .)
plot(h, . . .)
end
Update:
Without using axis handles
Make the app's axes handle accessible and current so that any newly plotted objects will be plotted to the App's axes.
1) Open the app in appdesigner. In Design View, from the Component Browser, select the main app figure at the top. In the Inpsector section below, go to Parent/Child and set HandleVisibility to 'on' or 'callback'. This step can also be done from within the code (see warning below).
2) Do the same for the axes within your app if needed (the default value may already indicate HandleVisibility 'on').
3) From within any callback function within the app, prior to plotting set the app's axes to current
axes(app.UIAxes)
% ^^^^^^^^^^ axis handle
4) call plotting function immediately afterward so that it uses the current axis.
Warning: Now your UIAxes will the accessible by any plotting function which may lead to accidentally clear the axes or making unintended changes to it.
To minimize that problem, set the app handle visibility programmatically before and after plotting.
% app.MyApp is the handle to your app figure
% app.UIAxes is the handle to your app axes
app.MyApp.HandleVisibility = 'on'; % or 'callback'
axes(app.UIAxes)
[ DO PLOTTING ]
app.MyApp.HandleVisibility = 'off'; % or 'callback'

5 comentarios

Neil Buchanan
Neil Buchanan el 29 de Nov. de 2019
That worked, thank you for your quick response!
hello, what if the function is more sofisticated?, for instance the output of an object oriented, etc ...
so, editing the original function will be a mess rigth?
in GUIDE is quite simple
axes(handles.axes1)%plot area in the GUI
complex_function(DEM)%DEM is any input
But how to do something similar in appdesigner?
seemd that matworks did the app designer less flexible
Adam Danz
Adam Danz el 22 de Abr. de 2020
Editada: Adam Danz el 22 de Abr. de 2020
@Jules Ray, I updated my answer for a solution to the problem you described.
The better solution is to always include an axis handle in all plotting function inputs. Many of the plotting functions I write contain an optional input for the axis handle and when empty or missing, the code uses gca().
% app.MyApp is the handle to your app figure
% app.UIAxes is the handle to your app axes
app.MyApp.HandleVisibility = 'on'; % or 'callback'
axes(app.UIAxes)
[ DO PLOTTING ]
app.MyApp.HandleVisibility = 'off'; % or 'callback'
This worked perfectly.
@Justin Namuco, if you're going to do that, it would be better to use onCleanup to turn HandleVisibilty off (shown below). The benefit of this is if the plotting code unexpectedly stops, the HandleVisibility property will still be set.
revertHandleVis = onCleanup(@()set(app.MyApp,'HandleVisibility','off'));
app.MyApp.HandleVisibility = 'on'; % or 'callback'
axes(app.UIAxes)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 26 de Nov. de 2019

Comentada:

el 8 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by