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?

 채택된 답변

Adam Danz
Adam Danz 2019년 11월 26일
편집: Adam Danz 2020년 4월 22일

2 개 추천

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

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 2020년 4월 22일
편집: Adam Danz 2020년 4월 22일
@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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2019년 11월 26일

댓글:

2022년 11월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by