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 个评论

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.
#Script1
X= [X1 X2]
Run(Script2)
#script2
X1 = input(enter a value)
X2= input(enter a value)
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 ...?

请先登录,再进行评论。

更多回答(2 个)

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 个评论

I’m not allowed to edit script2. I want to bypass the user input prompts by having the values entered in script1
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 Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by