MATLAB Answers

Is it possible to pre-define user input values and skip input dialogs in a script?

20 ビュー (過去 30 日間)
Thomas Murphy
Thomas Murphy 2019 年 10 月 7 日
コメント済み: Thomas Murphy 2019 年 10 月 19 日
I'm trying to work out a simple script right now that will enable us to run a sequence of 2-3 other Matlab extensions for Imaris (an image analysis program). My goal is to have a single prompt at the beginning of this script asking the user for the necessary inputs, which will then be used as parameters for the appropriate extensions, allowing the user to set up a lengthy sequence of analysis without needing to baby-sit the program.
The problem is that each of these extension scripts currently get their parameters via dialog prompts (e.g. inputdlg). I need to somehow suppress these dialog prompts and pass the appropriate values from my own script instead, but as far as I know there is no way to do this without modifying the code for each extension. I've tried to run a script in "batch" mode using the batch() function, which sounds like it might do what I want, but I can't seem to pass any variables to it, and I suspect I'm fundamentally misunderstanding the input arguments or perhaps the function overall.
Is there an easier method to do this? Or rather, is it even possible? Or am I better off just creating a custom version of each extension?

  2 件のコメント

Adam 2019 年 10 月 11 日
I would always recommend using functions rather than scripts, with explicit input arguments, but if you simply want to supress input dialogs you can just run an
if ~isempty( 'someVariableName', 'var' )
type check which will only launch the input dialog if the variable does not exist. Or you can combine isempty() in the test too if you want to support having empty variables as implying they need to be filled from an input dialog.
If you have a whole bunch of variables from the same input dialog you can store them all in a struct for the purposes of passing to your script/function, then you can just check if the struct exists to launch the dialog or not (Technically you'd want to check if the struct contains all the parameters too, depending how fussy you want to be - a class would obviously work better than a struct since it has defined properties)
Stephen Cobeldick
Stephen Cobeldick 2019 年 10 月 11 日
"Is there an easier method to do this?"
Forget about awkward dialog boxes, simply write functions with input arguments.
Functions with input arguments are readily expandable, trivially callable from other functions, can be automatically tested, don't waste time waiting for user interactions, make results easily repeatable, and sensibly leave it up the user do know where their data comes from.

Sign in to comment.


Steven Lord
Steven Lord 2019 年 10 月 11 日
Is there an easier method to do this? Or rather, is it even possible? Or am I better off just creating a custom version of each extension?
Possible? Perhaps, through some trickery involving shadowing inputdlg. But I would strongly recommend that instead you modify (or work with the extension authors to modify) the extensions to only ask for the inputs interactively if they did not receive inputs from their caller, via something like:
if nargin == 0
theInputs = interactivelyPromptForInputsUsingInputdlg();
This will preserve the existing behavior, where the extensions prompt when used interactively, while allowing you to pass information in and skip the prompt when used programmatically.

  1 件のコメント

Thomas Murphy
Thomas Murphy 2019 年 10 月 19 日
This sounds like a great compromise - no extensive reworking of the original code, and it retains the original functions so I don't have to keep two versions of each extension. Thanks for the help!

Sign in to comment.

その他の回答 (1 件)

Harsha Priya Daggubati
Harsha Priya Daggubati 2019 年 10 月 11 日
Here is the thread in which passing your inputs to a input dialog through a script is described.
Hope this helps you!

  0 件のコメント

Sign in to comment.

Translated by