Calling external functions in App Designer

230 次查看(过去 30 天)
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?

回答(3 个)

Ameer Hamza
Ameer Hamza 2018-5-1
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 个评论
Helder Barreto
Helder Barreto 2020-4-21
编辑:Helder Barreto 2020-4-21
Hello! I am new on matlab and I am making an app with app designer. I have also to call functions in separated files. So I have for example my file called "Euler.m" and when I am calling in a switch case but the appdesigner give me an warning to change the syntax. Its normal? I need to change something? If you can help me I would be glad,
Best Regards,
Hélder Barreto
Ameer Hamza
Ameer Hamza 2020-4-21
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.

请先登录,再进行评论。


Eric Sargent
Eric Sargent 2020-12-9
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:
  2 个评论
Gary Gorman
Gary Gorman 2023-2-11
Eric, i do not understand your comments. The issue of running a function file such as myFunc.m is not at all the same as running functions generally. I have a file myFunc.m in my working directory, the same directory as my app myAPP.mlapp. I can run myFunc.m in livescript from a Matlab window but I cannot run myFunc.m from AppDesigner. It appears that in order to use a file named myFunc.m which contains a function you must do some stuff in AppDesigner to facilitate that use, and that stuff that you must do goes beyond simply calling the function as is done in a livescript window. So what is the special stuff you must do to run a myFunc.m external function in Matlab?
Gary Gorman
Gary Gorman 2023-2-11
Never mind, Eric, i see it now. The 'special sauce' needed to run external functions in ".m" files is given by Ameer hamza's answer. In order to run an external function in AppDesigner you must redeclare the external function according to the syntax that Ameer describes. Probs "redeclare" is not the write language but I think it gets the idea across:)

请先登录,再进行评论。


SilverSurfer
SilverSurfer 2020-5-2
编辑:SilverSurfer 2020-5-2
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.
  1 个评论
Kelechi Akwataghibe
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?.
The external functions declared in .m files are public by default. This means that, they are seen by every class/file in matlab.
Declaring a different function with the same name elsewhere will result in a conflict.
Why the first argument shall be app?
This convention is established by MATLAB App Designer itself and is designed to represent an instance of the app.
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."
You can choose to make the method static since it doesn't require the class object 'app'. The following will show you how to: https://www.mathworks.com/matlabcentral/answers/1750735-create-static-method-in-app-designer
Your findsignals() function returns something which is stored in 'Output'. The second warning shows that you haven't yet used this result (Output) elsewhere.
In both cases, you can also choose to surpress the warnings (though not advised).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by