Pass variable to .m file from app designer
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
prabhjeet singh
le 1 Nov 2021
Modifié(e) : prabhjeet singh
le 22 Mar 2022
I am new in matlab app designer, but I have gone through basics of it. I have a very simple query where I am stuck at.
I am calling "backtest.m" file in app designer
%backtest.m file
clc;
clear;
close all;
z = x + 6;
And this is the app designer code
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
x = app.xEditField.Value;
backtest;
end
end
I get an error where x is undefined. What should I do to remove this error?
Thanks in advance.
Prabhjeet
3 commentaires
Réponse acceptée
Yongjian Feng
le 1 Nov 2021
Modifié(e) : Yongjian Feng
le 1 Nov 2021
Return z value from your funciton backtest.m:
function z = backtest(x)
z = x + 6;
end
Call this function from app designer like this:
z = backtest(x);
Basically you shall pass input/output to/from your function using input argument and return. Don't pass data using global variables.
6 commentaires
Ayman Shaban
le 18 Mar 2022
Thank you very much for your answer, i have another Q , how can i show the result of .m file on app designer after already calling the .m file
Plus de réponses (1)
Cris LaPierre
le 1 Nov 2021
This will never work as written. The clear inside the m-file, whether a script or function, will remove any variables, including the one passed in.
Remove the following from your m-file
clc;
clear;
close all;
Place the m-file in the same folder as your app. Now when you call it, it's the same as adding the code from your m-file to the code in the callback function.
Note that the variable z will only exist inside the callback function. You need to keep in mind your variable scope.
I do agree that turning this into a function makes more programatic sense to me, but it is not necessary to get it to work.
0 commentaires
Voir également
Catégories
En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!