How to assign a variable to an arbitrary value entered in command window

조회 수: 12 (최근 30일)
Neil Bhattacharjee
Neil Bhattacharjee 2016년 7월 28일
댓글: Walter Roberson 2016년 7월 28일
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

답변 (1개)

Steven Lord
Steven Lord 2016년 7월 28일
I assume this is a follow-on to this question?
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
Neil Bhattacharjee
Neil Bhattacharjee 2016년 7월 28일
편집: Neil Bhattacharjee 2016년 7월 28일
I understand that you can run functions from the command terminal .
It's just that due to the nature of the project we're working on, using a function just isn't a viable option.
Are yo sure there isn't a way to do this with a script?
Sorry for the pestering.
Walter Roberson
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by