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

19 vues (au cours des 30 derniers jours)
Neil Bhattacharjee
Neil Bhattacharjee le 28 Juil 2016
Commenté : Walter Roberson le 28 Juil 2016
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

Réponses (1)

Steven Lord
Steven Lord le 28 Juil 2016
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 commentaires
Neil Bhattacharjee
Neil Bhattacharjee le 28 Juil 2016
Modifié(e) : Neil Bhattacharjee le 28 Juil 2016
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 le 28 Juil 2016
Why not write a wrapper function that gets the values from the arguments, assigns them to variables, and then run's the script ?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by