Script to run another script, specifying a parameter's value
129 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Voss
el 6 de Mzo. de 2024
"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.
Respuestas (1)
Steven Lord
el 6 de Mzo. de 2024
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 comentarios
Steven Lord
el 6 de Mzo. de 2024
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.
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!