Assigning 'input values' to 'input variables' in Matlab App Designer

57 visualizaciones (últimos 30 días)
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras el 13 de Oct. de 2020
Comentada: Ameer Hamza el 13 de Oct. de 2020
I`m designing an app which could make the following:
The user enters whichever variables he wants from those[m,a,g,h,v] in the EditField(Text) box of the left and then he enters those variable's value in the correspondent EditField(numeric) of the right. First Image.
The point of that is that the app assigns to the variable entered, the value entered. As an example, if I enter whatever variable I want (for example 'm') in the left box and '10' in the right box, the point is that the program assigns to the variable 'm' the value of '10'. Like m=10. And so on. Second Image
So that the variable`s value gets defined by the user himself.
The issue here is that I don´t know how to assign those input values to those input variables. I have tried many things but I thing I miss something, cause matlab shows me many errors and it doesn't work.
image1
image 2

Respuestas (1)

Ameer Hamza
Ameer Hamza el 13 de Oct. de 2020
Editada: Ameer Hamza el 13 de Oct. de 2020
Although possible using eval(): https://www.mathworks.com/help/matlab/ref/eval.html, but I must warn you that eval is evil: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Creating variable name dynamically is never considered a good coding practice. It will make your code difficult to understand and hard to debug in the long run.
If you must do something like this. I suggest you to dynamically field inside a struct. At least that will reduce the chances of any unexpected side-effects in your code. For example
var_name = 'x'; % variable name input by user
val = 10; ; % value of variable
s.(var_name) = val
  2 comentarios
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras el 13 de Oct. de 2020
Thanks for your answer but what I am looking for is that the user enters in the app, whichever the variables he wants and whatever the values he wants, by editing those EditFields.
It may sound something simple, but the point is to design a calculator for phyisics, so the user could enter the variables he wants in the order he wants with the values he wants.
As an example of what I am looking for: The user enters the app and enters in those boxes the variables 'm=5' and 'a=10' and the program calculates the force 'F'.
Ameer Hamza
Ameer Hamza el 13 de Oct. de 2020
Ok, Here I will show how you can do it using eval() because it is simple. You can read about the struct() approach.
input1 = 'm=5';
input2 = 'a=10';
eval(input1);
eval(input2);
F = m*a; % F will be 50;

Iniciar sesión para comentar.

Categorías

Más información sobre Variables en Help Center 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