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개)

James Tursa
James Tursa 2018년 4월 12일
편집: James Tursa 2018년 4월 12일
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
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 Exchange에서 Simulation, Tuning, and Visualization에 대해 자세히 알아보기

질문:

2018년 4월 12일

편집:

2018년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by