How to assign a variable to an arbitrary value entered in command window
7 ビュー (過去 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!