How to run predefined user inputs for a script inside a script?
Mostrar comentarios más antiguos
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 comentarios
James Tursa
el 12 de Abr. de 2018
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
el 12 de Abr. de 2018
Editada: James Tursa
el 12 de Abr. de 2018
James Tursa
el 12 de Abr. de 2018
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 ...?
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 12 de Abr. de 2018
1 voto
James Tursa
el 12 de Abr. de 2018
Editada: James Tursa
el 12 de Abr. de 2018
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 comentarios
Lebarian Stokes
el 12 de Abr. de 2018
James Tursa
el 12 de Abr. de 2018
Editada: James Tursa
el 12 de Abr. de 2018
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.
Categorías
Más información sobre Simulation, Tuning, and Visualization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!