Pass variable to .m file from app designer
이전 댓글 표시
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
Yongjian Feng
2021년 11월 1일
편집: Yongjian Feng
2021년 11월 1일
Your .m shall be a function:
function backtest(x)
z = x + 6;
end
Then call it as backtest(x) from app designer.
prabhjeet singh
2021년 11월 1일
Yongjian Feng
2021년 11월 1일
Please check my answer below.
채택된 답변
추가 답변 (1개)
Cris LaPierre
2021년 11월 1일
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.
카테고리
도움말 센터 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
