How do I communicate between an AppDesigner app and a function

8 views (last 30 days)
I have been using the AppDesigner to create my UI. Since I find writing the actual functions much less convenient in AppDesigner than in the 'normal' MatLab class Editor, I would like to place all the functions I can in there. However, I cannot figure out how I can set values from AppDesigner controls from my Editor functions. For example, I want to create three colour histograms in the app and would like to send the data for that from the function placed in the 'normal' MatLab Editor. How do I do that? I locked up setappdata/getappdata but that does not seem to work?!

Answers (1)

Mukul Rao
Mukul Rao on 18 Jul 2017
Hi,
Here is a simplistic template to illustrate the workflow to access App Designer plot data:
  • Create an axes for your application by dragging an "axes" component from the component library
  • In the App Designer "Code View", you will notice that a new "UIAxes" property is created and it has public access.
  • Right click on the app in Design View - Callbacks -> add startupFcn
  • Within the body of the "startupFcn" function, in code view, place the following line of code
histogram(app.UiAxes,randi([1 10],100)) %Plots a histogram of random integers on app startup
  • Save the application as "myapp.mlapp".
  • Instantiate the application either in a script or command line :
app = myapp
  • You can pass the "app" handle to any function created with the MATLAB editor as an input argument and basically modify the data property of the histogram to update its value
function myFunction(app)
%Do some work
app.UIAxes.Children.Data = newData;
% Note: Children might contain more than just a histogram if you have other plots inside the axes
end
  1 Comment
Cris LaPierre
Cris LaPierre on 6 Feb 2019
This documentation page will walk you through how to do this. There is also an example at the bottom of the page you can open and inspect.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer 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!