How to call a script when clicking on a button in matlab app.designer?

I am trying to do the following app:
In which, when clicking on the button, a script is executed and its numerical calculations performed and its result appears in the white window
I have defined as private property the following variables with those values.
So when the button is clicked, the script (which name is: operasion.m) is called and the result (j) appears on the white window
The script is the following
Thank you very much for the help. By the way, the script.m and the app are saved in the same folder

 Respuesta aceptada

You run a script by calling it by name.
However, is there a reason you don't just include the code and parameters inside your app? It seems unnecessary to have this code in an external m file.
app.solu.Value= app.a + app.b;

8 comentarios

Thanks for your reply
Obviously this example is so so so so simple and it could be done as you said. The point is that "in reality" I am working on a project on which I need to call a script (which is way more complicated than that one) in app.designer by clicking a button.
But to simplify the question in the forum, to keep it simple right to the issue and to do not disturb with irrelevant information, I have made this simple script and app example.
However, I already tried putting in the Button's callback the name of the script and the name of the script.m as you said and it doesn't work.
First try:
and its error:
Second try:
and its error:
We only know what you share. It's better to share your actual example. Invariably, when you try to simplify it, important detils are lost.
Do not include the file extension. Just use operasion.
As for the first error, your script cannot run because the variables a and b do not exist in the workspace. All your callbacks are functions. You need to keep variable scope in mind.
If you will only run this script in your app, use app.a and app.b instead of a and b. If not, then consider turning your script into a function, that accepts a and b as inputs, and returns value j.
function j = operasion(a,b)
j=a+b;
end
Inside your app, you would call the function and pass it inputs of app.a and app.b, and capture the output in app.j (the actual names can be different from what is used in the function).
app.j = operasion(app.a,app.b)
Thanks for your reply!
In one hand, I have used app.a and app.b values inside the script (instead of a and b) and it doesn't work.
In the other hand:
My real project is that one:
On the left the user enters the number of inputs you want and know (and its values) and on the right the number of outputs you want to know the value of. By default, the values are "9999" (which I considered as 9999 as 6969 as invalid ones).
The inputs and outputs can be: F,Em,Ec,m,g,h,a,v
The point is that you enter whatever and whichever inputs you want and its values and when you click the button, the program calculates what it can according to that information.
Knowing that:
F=m*a
Em=m*g*h
Ec=(1/2)*m*(v^2)
Which corresponds to the following function.m respectively:
p_fuer.m
p_enmecm
p_encin.m
So the script (named: programphysic) that sees what values are known and which ones are not and can be calculated and calculates them and appears in the output windows.
The script is the following:
if m==6969&&F~=9999&&F~=6969&&a~=9999&&a~=6969
m=9999;
[F,m,a]=p_fuer(F,m,a)
elseif m==6969&&Em~=9999&&Em~=&&g~=9999&&g~=&&h~=9999&&h~=6969
m=9999;
[Em,m,g,h]=p_enmec(Em,m,g,h)
elseif m==6969&&Ec~=9999&&Ec~=&&v~=9999&&v~=6969
m=9999;
[Ec,m,v]=p_encin(Ec,m,v)
end
if F==6969&&m~=9999&&m~=6969&&a~=9999&&a~=6969
m=9999;
[F,m,a]=p_fuer(F,m,a)
end
if a==6969&&F~=9999&&F~=6969&&m~=9999&&m~=6969
m=9999;
[F,m,a]=p_fuer(F,m,a)
end
if Em==6969&&m~=9999&&m~=6969&&g~=9999&&g~=6969&&h~=9999&&h~=6969
m=9999;
[Em,m,g,h]=p_enmec(Em,m,g,h)
end
if g==6969&&m~=9999&&m~=6969&&h~=9999&&h~=6969&&Em~=9999&&Em~=6969
m=9999;
[Em,m,g,h]=p_enmec(Em,m,g,h)
end
if Ec==6969&&m~=9999&&m~=6969&&v~=9999&&v~=6969
m=9999;
[Ec,m,v]=p_encin(Ec,m,v)
end
if v==6969&&Ec~=9999&&Ec~=6969&&m~=9999&&m~=6969
m=9999;
[Ec,m,v]=p_encin(Ec,m,v)
end
if h==6969&&Em~=9999&&Em~=6969&&g~=9999&&g~=6969
h=9999;
[Em,m,g,h]=p_enmec(Em,m,g,h)
end
So, the point and the issue I have is that when you click on the button CALCULATE, the matlab.app runs the script.
Sounds like the perfect case for using a user-defined helper function inside app designer.
Not sure what you are using the dropdowns for. You could probably turn them into labels. Name your edit fields appropriately, and you can just use the properties to get the values. Since all of these are part of the apps structure, and apps is the default first input to your custom function, it should be pretty straight forward.
function j = operasion(app)
a = app.aEditField.Value;
b = app.bEditField.Value;
j = a+b;
end
Thanks for your reply!
But I also have a bunch more functions written on more scripts that I would like to introduce in the app (more than 50 functions on 4 or 5 more scripts that I have prepared). I think it would be a lot more easier and comfortable for me to directly execute or call a script in the app rather than start now re-writing on the app each function one by one.
There is any way of executing a matlab script by clicking the button?
Yes. I thought we already discussed this. See here.
Your script is running. You now have a syntax error because the variables you use in your script do not exist in the function workspace. You have to create the variable a before your script can use it.
You can update your scripts to use app.a, which you appear to not want to do. You can convert your scripts to functions so you at least don't have to add app to every variable. You also don't want to do that. You are left with the most brute-force solution. Create every variable in your callback before running your script.
a = app.a;
b = app.b;
operasion
app.solu.Value = j;
I have also to rename j as follows:
j=app.j;
And yes, It worked!
Thank you very much!!
At least based on the original example you don't have to do j=app.j since j is calculated in the script. If you want to save the value in your app, you probably want to do the opposite.
app.j = j;

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by