User input as a variable.
89 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I have a pretty basic question here! I’m writing a script asking the user for an input and I want to use that input (string) as a variable and assign it a value. Basically, I want the user to name the variable and I will assign the values? Here is an illustration and my code
X = 100;
First_input = input('Please Enter your first variable\', 's');
Second_input = input('Please Enter your first variable\', 's');
Third_input = input('Please Enter your first variable\', 's');
(place_holder_for_whatever_the_first_input_is) = 1000 * x;
(place_holder_for_whatever_the_Second_input_is) = 2000 * x;
(place_holder_for_whatever_the_Third_input_is) = 1000 * x
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/689408/image.png)
0 个评论
采纳的回答
Walter Roberson
2021-7-19
We firmly recommend against that. http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
... for example what would happen if the user happened to name 'X' or 'x' as the variable, interfering with your use of that in your code?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!