How to assign a variable to an arbitrary value entered in command window
14 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Steven Lord
on 28 Jul 2016
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 Comments
Walter Roberson
on 28 Jul 2016
Why not write a wrapper function that gets the values from the arguments, assigns them to variables, and then run's the script ?
See Also
Categories
Find more on Debugging and Analysis 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!