How to run predefined user inputs for a script inside a script?

I am trying to run a script(script2) multiple times inside another script(script1). Script2 has multiple user input prompts. If I want to predefine the inputs in script1, how can I get script2 to run those values?

3 件のコメント

James Tursa
James Tursa 2018 年 4 月 12 日
Please provide a short example of the code you are currently using, and then tell us what it is doing that you don't like, and then what you would like the behavior to be.
Lebarian Stokes
Lebarian Stokes 2018 年 4 月 12 日
編集済み: James Tursa 2018 年 4 月 12 日
#Script1
X= [X1 X2]
Run(Script2)
#script2
X1 = input(enter a value)
X2= input(enter a value)
James Tursa
James Tursa 2018 年 4 月 12 日
So, you have provided the code. Thanks! But you have not clearly told us what you want the behavior to be. Do you want script2 to only ask for inputs if X1 and X2 do not already exist? Or ...?

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

 採用された回答

Lebarian Stokes
Lebarian Stokes 2018 年 4 月 12 日

0 投票

Ok. Thank you for your input

その他の回答 (2 件)

James Tursa
James Tursa 2018 年 4 月 12 日
編集済み: James Tursa 2018 年 4 月 12 日

0 投票

Is this what you want?
%script2
if( ~exist('X1','var') )
X1 = input('enter a value for X1: ')
end
if( ~exist('X2','var') )
X2 = input('enter a value for X2: ')
end
EDIT:
If you can't modify script2, then you can take an all or nothing approach and shadow the input( ) function with a do-nothing function of your own. You will get warnings about output arguments not being assigned, but your X1 and X2 variable values will remain intact. This will only work if you pre-assign all variables that use the input( ) function in script2. I.e., you could have this function:
function x = input(s)
end
When script2 calls the input( ) function, it will do nothing and will not assign values to X1 or X2, so they will retain their values you gave them from script1. To be safe, put your version of input( ) somewhere where only script2 can see it so you are not messing up other code.

2 件のコメント

Lebarian Stokes
Lebarian Stokes 2018 年 4 月 12 日
I’m not allowed to edit script2. I want to bypass the user input prompts by having the values entered in script1
James Tursa
James Tursa 2018 年 4 月 12 日
編集済み: James Tursa 2018 年 4 月 12 日
If you can't edit script2 then you can't alter its behavior beyond what script2 is already programmed to do. I suppose you could shadow the input function with one of your own that checks for existence, but how would you know what variable the input was asking for? I don't see a viable approach for this unless the string argument to input contained the variable name itself.

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

カテゴリ

ヘルプ センター および File ExchangeSimulation, Tuning, and Visualization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by