Script to run another script, specifying a parameter's value
古いコメントを表示
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 件のコメント
Manikanta Aditya
2024 年 3 月 6 日
Hey,
In MATLAB, you can use the assignin function to assign a value to a variable in a different workspace.
Here’s a simple example of how you can do this:
% Parent script
param_value = 10; % The value you want to pass to the child script
assignin('base', 'param', param_value); % Assign the value to a variable in the base workspace
run('child_script.m'); % Run the child script
In the child script, you can then access the variable param directly:
% Child script
disp(param); % Display the value of param
In this example, the parent script assigns a value to the variable param in the base workspace, which is accessible by all scripts. Then it runs the child script, which can access the value of param.
Thanks!
"Is there a simple way..."
Yes: write functions (not scripts) and efficiently pass the values as input/output arguments.
"... 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?"
Ugh, avoid messing around in other workspaces like that. But this comment makes it unclear what you are actually doing: scripts all share the same workspace (where they are called from), so perhaps you are already using functions?
fralab
2024 年 3 月 6 日
Manikanta Aditya
2024 年 3 月 6 日
It seems like the child script might not be recognizing the variable from the base workspace. Here’s an alternative approach using a function instead of a script for the child script. Functions have their own workspaces, so you can pass the parameters directly to them.
Here’s how you can modify your parent and child scripts:
Parent script:
% Parent script
param_value = 10; % The value you want to pass to the child script
child_script(param_value); % Call the child function with the parameter
Child script (now a function):
% Child function
function child_script(param)
disp(param); % Display the value of param
end
In this example, the parent script calls the child function directly and passes the parameter as an argument. The child function then receives the parameter and can use it within its own workspace. This should resolve the issue of the unrecognized variable.
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
2024 年 3 月 6 日
回答 (1 件)
Steven Lord
2024 年 3 月 6 日
0 投票
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 件のコメント
fralab
2024 年 3 月 6 日
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
2024 年 3 月 6 日
カテゴリ
ヘルプ センター および File Exchange で System Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!