How to run a matlab function using a button pushed in AppDesigner

조회 수: 19 (최근 30일)
Myssipsa Mehraz
Myssipsa Mehraz 2021년 7월 19일
댓글: Myssipsa Mehraz 2021년 7월 22일
Hello everyone,
My question is simple. I want to run a function in matlab base workspace using a button pushed in AppDesigner.
I added a button in my app, and create a callback with this instruction :
run ('Path of my function');
My function is (just for the test) :
function r=addition(a,b)
r=a+b;
end
Thank you in advance.
  댓글 수: 6
Jan
Jan 2021년 7월 21일
Using assignin to create variables in the base workspace and calling a script is a shot in your knee. Convert the script into a function and provide the variables as input arguments. Then it is very easy to call your function from inside the callback of a GUI.
Myssipsa Mehraz
Myssipsa Mehraz 2021년 7월 22일
I tested it and it works. Thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2021년 7월 19일
편집: Matt J 2021년 7월 19일
The typical way would be to invoke addition() from your callback the way you would from any ordinary function,
function ExecuterButtonPushed(app,event)
a=app.aEditField.Value;
b=app.bEditField.Value;
r=addition(a,b);
end
  댓글 수: 6
Myssipsa Mehraz
Myssipsa Mehraz 2021년 7월 21일
@Jan : Thank you for your answer. I posted a comment in the answer of @Matt J where i explain more what i need to do. Maybe it will be more clearly with this explanations.
Matt J
Matt J 2021년 7월 21일
편집: Matt J 2021년 7월 21일
Maybe it will be more clearly with this explanations.
I'm afraid not. What you have not explained is why the machine you are controlling needs to receive its instructions from a Matlab script as opposed to an mfunction. As Jan points out, you could easily convert your script to a function and invoke that function directly from the callback, as my original answer suggests.
Also, even if you insist on using a script, it is not clear what problems you are currently experiencing by running your script from the callback. I can only guess it is because there are additional variables that the script needs besides the EditField values and which reside in the base workspace. If so, why not just bring those extra variables into the app where the callback can see them? Make them inputs to the app constructor and store them in properties.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by