Can one run a matlab script from the command line and pass arguments to it **without making it into a function**?

22 ビュー (過去 30 日間)
I was to run a MATLAB script from the terminal and pass arguments to it. I know I can make it into a function to pass arguments to it but I don't want to make it into a function. Is there a different way? Like can one get the environment variables from the terminal or something as an alternative? As in:
SLURM_ARRAY_TASK_ID = int(os.environ['SLURM_ARRAY_TASK_ID'])
SLURM_JOBID = int(os.environ['SLURM_JOBID'])
as it would be done in python?
  2 件のコメント
Brando Miranda
Brando Miranda 2017 年 12 月 11 日
follow up question:
https://www.mathworks.com/matlabcentral/answers/372388-why-doesn-t-getenv-work-on-mac-os-x
Stephen23
Stephen23 2017 年 12 月 12 日
編集済み: Stephen23 2017 年 12 月 12 日
"...but I don't want to make it into a function."
Functions have many advantages over scripts (faster, encapsulated functionality, freedom from interference, clear workspace, debugging), which is why experienced MATLAB users write functions and not scripts. Why do you need to avoid writing better code? Why do you want to waste time avoiding doing the one thing that would actually solve your problem very neatly?

サインインしてコメントする。

回答 (2 件)

Greg
Greg 2017 年 12 月 11 日
編集済み: Greg 2017 年 12 月 11 日
A script has access to its caller's workspace. You need not pass anything, they'll just be there.
This also means all variables and changes to variables you make inside the script happen in the calling workspace.
  1 件のコメント
Brando Miranda
Brando Miranda 2017 年 12 月 11 日
Im running it from the terminal...there is no workspace until the script is called. So it comes from the bash script

サインインしてコメントする。


James Tursa
James Tursa 2017 年 12 月 11 日
編集済み: James Tursa 2017 年 12 月 11 日
You could have your script use the getenv( ) function to retrieve all of those environment variables (as strings). Or have your script call another script as the first action, and that other script does all of the getenv( ) stuff (and perhaps also conversion of string to numeric as desired).
  5 件のコメント
Brando Miranda
Brando Miranda 2017 年 12 月 11 日
I guess I could XD Its so silly though, why do I need so many scripts to call the original script?
Greg
Greg 2017 年 12 月 12 日
Because you arbitrarily refuse to make it a function.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeThird-Party Cluster Configuration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by