How to assign a variable to an arbitrary value entered in command window
10 次查看(过去 30 天)
显示 更早的评论
Let's say you enter the number "2" into the command window.
I would like a program that assigns a variable "x" to whatever was entered in the command window.
I don't want to prompt the user for input. Ideally, the command window line would look something like this.
run program_file.m , 2, 3 , 4
I want the values 2 3 and 4 to be assigned to variables x y and z. Eventually I also want to be able to do this with strings, not just integers.
I do not want to run the program before I enter the numbers next to it. I want the program to run with the numbers and string (hopefully) on the same line.
I know this seems unnecessarily cumbersome and illogical due to the fact that I just could prompt user for input.
Thanks for the help,
Bubby
0 个评论
回答(1 个)
Steven Lord
2016-7-28
The run function runs script files, not function files. Script files cannot have input arguments. The scenario you're describing is EXACTLY what functions were designed to do!
function q = program_file(x, y, z)
q = x+y.^2+z.^3;
Now call program_file as:
q = program_file(2, 3, 4)
Inside program_file, the variable x will have the value 2, the variable y will have the value 3, and the variable z will have the value 4.
4 个评论
Walter Roberson
2016-7-28
Why not write a wrapper function that gets the values from the arguments, assigns them to variables, and then run's the script ?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!