Can I run a function and automatically provide it with user input?

I have a function that I need to run, but I am not allowed to change the code inside.
The script asks for user input through the use of popup dialog boxes using commands like
inputdlg()
uigetfile()
uigetdir()
function PVAnalysisMain()
[files, path] = uigetfile('.csv', "Select Peak Valley Data file to Analyze", 'MultiSelect','off'); %get location of PV file
fileName = fullfile(path, files);
dataPath = uigetdir(fileName, 'Select where to save data'); %gets folder to save data too
prompt = 'eg. 5%';
dlgtitle = 'What percentage load drop is defined as fracture?';
definput = {'5', 'double'};
thres = inputdlg(prompt, dlgtitle, [1 35], definput);
thres = str2double(thres);
%other analysis
end
Is it possible to write code that will run PVAnalysisMain(), and when the function asks for user input, give it some predetermined value for the input? I need to run this function on up to 30 files multiple times so if I can run it in a loop that would make my life easier

8 Comments

‘... give it some predetermined value for the input?
That is what ‘definput’ does in the inputdlg call. It fills in the defaults, so if nothing is entered by the user, the default values are returned in the cell array of strings output.
Right, but I'm asking if I can run this function in a loop without needing to interact with every dialog box that pops up, when the function gets called. Also the uigetfile call wouldn't have a default input to give it.
What do you mean you're "not allowed" to change the code. What would happen if you did?
This code is part of a version controlled repository, so it is validated by a government agency to work as intended. Rewriting the code for a slightly different use case and getting it validated is time and labor intensive.
@Ojaswi: You might look into using a java.awt.Robot to automate the necessary text entry and mouse clicks. It may not make your life easier though (at least, not in the short term, but maybe in the long term).
Does the "other analysis" section simply call one function with the inputs that the user is supposed to have entered into the dialogs? If so I'd call that function directly instead of going through the dialog-heavy function.
If it doesn't, is extracting that section of the code to another function (and having this one call that function) a small enough change that the validation process wouldn't be so bad?
There are other approaches but they're workarounds. The right solution would be to break this up into an interface piece (with the dialogs) and a computational piece (that can be called directly.)
@Ojaswi, I understand the problem. The simple straight answer is no. As far as I know, there is no way to re-direct "user inputs" as you wanted. That is why it is always better to provide an API for the GUI program.
This function could be easily re-written to accept various input arguments. If a certain input argument, e.g. FileName, is not provided. Then inside the function, enable the uigetfile() to get it.

Sign in to comment.

 Accepted Answer

Is it possible to write code that will run PVAnalysisMain(), and when the function asks for user input, give it some predetermined value for the input?
Yes. You can write your own functions such as uigetfile() and uigetdir() that you place earlier in the MATLAB path, and which appropriate return values automatically. In order to avoid interfering with the built-in functions, the replacement functions should somehow detect which context the functions are being called in, and re-implement the built-in function for those cases.
This code is part of a version controlled repository, so it is validated by a government agency to work as intended.
What is intended by the code is to prompt the user for those values, so anything you do to automate the process will violate the validation conditions.
Is this all for setting up test cases? Because if so then perhaps there are some things you can do within the automated test framework.

1 Comment

Thanks Walter. Sounds like the answer is no not really unless I play fast and loose with what exactly was validated.
This is for running analysis on a set of experimental data using a set of different input conditions. The data set is not large enough where doing it manually would be untractable.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 3 Mar 2023

Commented:

on 3 Mar 2023

Community Treasure Hunt

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

Start Hunting!