Calling external functions in App Designer

747 views (last 30 days)
Joshua Brophy
Joshua Brophy on 30 Apr 2018
Edited: Eric Sargent on 9 Dec 2020
For a final project in a MATLAB class, we're required to create a modular program using App Designer to plot data points. We've written functions in the regular MATLAB Editor that authenticate a call to Twitter's Standard Search API, make the call, and deliver relevant information in variables to plot. All the code runs exactly how we want it to within the MATLAB command window. The trouble we're having is in figuring out how to call our functions within the App Designer. If I go into the "functions" tab within the code browser and try to add a new one, it doesn't let me add a function that doesn't have "app" as one of the input arguments. I heard elsewhere that it should be possible to call functions you've already written using the Editor, but it's unclear where to actually type in the call to these functions. I just want to be able to use the same output variables from the functions we've already written, and display the relevant data using tables and plots in the designer. Any advice on how to do that?

Answers (3)

Ameer Hamza
Ameer Hamza on 1 May 2018
if you have some functions placed in you MATLAB path, it is not necessary to paste them in the app designer code. You can just call them like normal functions. For example I’d you have written a function myFunction In MATLAB editor. You can call it in app designer
function appDesignerFunction(app)
Output = myFunction(Input);
end
  4 Comments
Ameer Hamza
Ameer Hamza on 21 Apr 2020
Barreto, It means that there is also a property in your app named Euler(). It will not cause any error, but it can cause confusion when you are trying to debug.

Sign in to comment.


Eric Sargent
Eric Sargent on 9 Dec 2020
There is MATLAB documentation to help answer this:
But, to anwer your question here:
This sounds like a general MATLAB question and is not just specific to App Designer. Please see this post about how to use functions from other folders outside of you current directory. This approach will work the same with an App Designer app to call the function.
The difference is you will (probably) need to define the function outputs as properties of the app, and then reference those properties using "app.<variablename>" in other functions / callbacks within your app.
Please see my answer on this post related to referencing properties within App Designer and/or visit the relevant documentation to learn more:

SilverSurfer
SilverSurfer on 2 May 2020
Edited: SilverSurfer on 2 May 2020
It is not really clear to me how to call external functions defined in .m files.
I have a function which search for some signals in an excel file. It has as output the list of signals found and as a input it has the full path of the file to read
function signals_final = findsignals(file_to_read)
In order to use it in the app designer I should define a new function using Function > Public function.
By looking at Ameer example it is possibile to declare it with a different name (appDesignerFunction). It is mandatory or can I use the same name as the .m file?. Why the first argument shall be app?
function appDesignerFunction(app)
Output = myFunction(Input);
end
Going back to my function I could write in the appdesigner
function findsignalsfunc(app)
Output = findsignals(Input);
end
Declaring in this way the designer get me two warnings
"Argument 'app' is unused. Should this method be Static?"
"The value assigned to variable 'Output' might be unused."
I have just tried to call the function without any declaration in the appdesigner and it works.

Community Treasure Hunt

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

Start Hunting!