Assigning 'input values' to 'input variables' in Matlab App Designer
20 次查看(过去 30 天)
显示 更早的评论
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/379431/image1.png)
![image 2](https://www.mathworks.com/matlabcentral/answers/uploaded_files/379436/image%202.png)
0 个评论
回答(1 个)
Ameer Hamza
2020-10-13
编辑:Ameer Hamza
2020-10-13
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 个评论
Ameer Hamza
2020-10-13
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;
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!