Assigning 'input values' to 'input variables' in Matlab App Designer
57 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (1)
Ameer Hamza
le 13 Oct 2020
Modifié(e) : Ameer Hamza
le 13 Oct 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 commentaires
Ameer Hamza
le 13 Oct 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;
Voir également
Catégories
En savoir plus sur Variables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!