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
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!
Stephen23
Stephen23 2024 年 3 月 6 日
編集済み: Stephen23 2024 年 3 月 6 日
"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
fralab 2024 年 3 月 6 日
Thanks for your replies :)
@Manikanta Aditya I've tried doing what you're saying (which made sense) but I get unrecognized variable error from the child script (of course by executing the parent one). The run function also gives a " Error in run (line 91) |evalin('caller', strcat(script, ';')); " error, if I try launching the child script without the run function it only gives the unrecognized variable error
@Stephen23 Unfortunately I'm still writing scripts, and I'm quite certain it's a script and not a function. The parent script launches the child script like you would manually do from Matlab's command line (I guess), but even if I see my parameter value assigned in the workspace panel, if I launch the child script it gives the unrecognized variable error.
Also the child script hasn't really got an output (well yes, but it's a text file) so I don't know how to deal with that either
Manikanta Aditya
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.
fralab
fralab 2024 年 3 月 6 日
編集済み: fralab 2024 年 3 月 6 日
@Manikanta Aditya thanks again. When I tried your second solution it said I was trying to used a cleared variable. So I deleted the "clear" command I had at the beginning of the child script and now both your solutions do work :)
I'd love to mark your comment as a solution but apparently I can't
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 日

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
fralab 2024 年 3 月 6 日
Thanks for your reply. I've seen many suggestion as this one of yours, but I don't think I understand really how to do that :(
The data I need to pass to the child script it just one number really, it seems to me that many solutions are way too complex for this simple little task heh
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

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

カテゴリ

ヘルプ センター および File ExchangeSystem Commands についてさらに検索

製品

リリース

R2023a

質問済み:

2024 年 3 月 6 日

コメント済み:

2024 年 3 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by