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

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 Comments

This function has 2 inputs. Hwich inputs do you provide when you define the callback?
I'm not sure I understand your question, but this is my code in AppDesigner and below the interface I created.
Thanks.
@Myssipsa Mehraz There's no reason the callback code you've posted wouldn't work. It's just an unnecessarily awkward style of programming.
Thank you for answer, but the code I posted is not the code i'm using, it's just a simple example. In my case, i'm developping an graphic interface to control a machine. And i have some functions that realizes the displacement of a support in the machine. For the moment, to do the move I want, I have to use the interface to indicate the values and positions, after that I report them to the base workspace with assignin command and then I execute the predefined code. This method is not very pratical, so I wanted to run this code from a button in the graphic interface directly.
I am attaching an image of the interface and a part of the code of the function so that you can better understand my problem.
%This function performs the movement needed to make measurements according to a cylindrical shape with a circular section
%with the rotation of the azimuth motor
function [vect_phi,vect_x,vect_z]= meas_cylindrical_circular(mot,motAz,rho,X_center,X_step,Y_center,H,Z_center,Z_step,phi_step)
% Computation of the limits
X_end = X_center - rho;
Z_end = H + Z_center;
% Vector declaration
vect_phi= 180:phi_step:-180;
vect_z = Z_end:Z_step:Z_center;
vect_x = X_center:X_step:X_end;
% Taking the probe to the first measurement point
move_x(mot,X_center);
move_y(mot,Y_center);
move_z(mot,Z_end);
% Measure and move
for interm_phi=1:1:length(vect_phi)-1
% Azimuth motor
move(motAz,vect_phi(interm_phi));
pause(5);
% Displacement along the x axis for the high horizontal section of the cylinder
for interm_x= 1:1:length(vect_x)
move_x(mot,vect_x(interm_x));
pause(5); % Break during measurement time
end
% Displacement along the z axis
for interm_z=1:1:length(vect_z)
move_z(mot,vect_z(interm_z));
pause(5); % Break during measurement time
end
% Back to the starting point // RESPECT ORDER OF MOVING FOR SECURITY
move_z(mot,Z_end);
pause(4);
move_y(mot,Y_center);
move_x(mot,X_center);
end
% Back to absolute zero // RESPECT ORDER OF MOVING FOR SECURITY
move_z(mot,0);
move_y(mot,0);
move_x(mot,0);
home (motAz);
So the Validate button allows me to take the values of the variables in the editfields to the base workspace as global variables and then I run the function. I want the "launch" button to execute the function after pressing the "validate" button.
By the way, I found this forum which talks exactly about what I need but it didn't work for me.
https://fr.mathworks.com/matlabcentral/answers/465219-using-app-designer-how-do-i-execute-m-files-in-the-main-workspace
Thank you in advance.
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.
I tested it and it works. Thank you.

Sign in to comment.

 Accepted Answer

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 Comments

This is just an exemple. In reality, I have to run a machine with a function i defined for it, and i want to launch this machine and its movement without executing the function in matlab but with pressing a button.
I just want to know how to do the link between the button and a function in matlab workspace.
@Myssipsa Mehraz: Matt's example does show you, how the function addition is called from inside the callback. This should solve your problem.
@Jan No, this didn't solve my problem. In my case i want to run the function which is in the workspace by pressing a buttonPushed in AppDesigner. So this is not a solution for me.
I know that there's a way to run matlab functions by pressing buttons in AppDesigner without invoking the function in the callback of the ButtonPushed.
Functions are not "in a workspace", but only variables. Remember, that each function has its own workspace, so there is no "the" workspace.
I does not make a sense to me to "run a Matlab function" triggered from a GUI element, without calling it from the callback function. In ancient Matlab versions the callbacks could be defined as CHAR vectors and then they are eval'ed in the base workspace, but this was prone to bugs and hard to debug.
I really do think, that Matt has posted the solution already. So please think about it a third time. If this does not match your needs, please explain exactly again, what you want to achieve.
In your screenshot you show a method to run a script in the base worksspace. This is a fragile approach, because scripts can interfere with the caller in many ways. Prefer to use functions instead, which have a clean list of inputs and outputs.
@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.
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!