Script to run another script, specifying a parameter's value

128 次查看(过去 30 天)
Hi, I am trying to write a simple (parent) Matlab script that should launch a sequence of macros of different programs.
The first one in the sequence would be a(nother) Matlab script I wrote (child), which prints a text file that is then read from the second macro, and so on.
The main goal is to repeatedly execute the parent script (hence the whole macros sequence) for an optimization process through Hyperstudy.
I have not reached the Hyperstudy step yet, but for now I would like the parent script to specify one parameter's value for the child script to use.
The child script is fairly complex and works with many parameters, but I'm interested in externally specifying only one (eventually through Hyperstudy, which should read the results of the whole macros sequence and then optimize the parameter and replay the sequence)
I've tried to learn how to do this from the many available Community and Support explanations but I can't get it. Is there a simple way to make the parent script tell the child script the value of the parameter to use, or for the child script to read the parameter's value from the parent's workspace?
Thanks to anyone who will try to help :)
  7 个评论
Voss
Voss 2024-3-6
"I deleted the "clear" command I had at the beginning of the child script"
@fralab: As Stephen pointed out, "scripts all share the same workspace (where they are called from)". clear clears that workspace, which of course removes all the variables defined in the parent script, since the parent script uses the same workspace as the child script.
fralab
fralab 2024-3-6
@Voss yup, I still had the clear command in the child script since it was the first time I was trying this stuff. Now the clear command is ran from the parent script's first line rather than the child's

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2024-3-6
Is there a simple way to make the parent script tell the child script the value of the parameter to use, or for the child script to read the parameter's value from the parent's workspace?
Generally, when faced with the scenario you described I'd make my programs into functions rather than scripts and pass the data between the functions as input and output arguments. That way any local variables the functions need to create (but don't need to share with other functions) stay in their own workspaces, avoiding cluttering those other functions' workspaces with unnecessary "stuff".
If there is a lot of data that needs to be passed between the functions, instead of defining functions with many input and/or output arguments, I'd pack that data into some sort of container (struct array, table array, cell array, or a class object) and pass the container between the functions. [Think of the containers like a suitcase; it's a lot simpler to carry and to check a suitcase when you're going on a trip than carrying or checking each individual piece of clothing would be.]
  3 个评论
Steven Lord
Steven Lord 2024-3-6
Take a look at the very first code segment in the first link I posted. It defines a very simple function that accepts one input argument and returns one output argument. In that first example, what goes into the function is known by the name n inside the function and what comes out of the function is the data in the variable f inside the function.
Once you know what needs to go into each piece of code and what needs to come out, just add that function definition line.
fralab
fralab 2024-3-6
I now understand what I was missing. Since my function doesn't have an output value I couldn't quite wrap my head around it but now I got it. thanks

请先登录,再进行评论。

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by