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

24 views (last 30 days)
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras on 13 Oct 2020
Commented: Ameer Hamza on 13 Oct 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.
image 2

Answers (1)

Ameer Hamza
Ameer Hamza on 13 Oct 2020
Edited: Ameer Hamza on 13 Oct 2020
Although possible using eval():, but I must warn you that eval is evil: 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
Ameer Hamza
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';
F = m*a; % F will be 50;

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!