Assigning 'input values' to 'input variables' in Matlab App Designer
56 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Ameer Hamza
on 13 Oct 2020
Edited: Ameer Hamza
on 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 Comments
Ameer Hamza
on 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;
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!