Can I run a function and automatically provide it with user input?
Show older comments
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
Star Strider
on 3 Mar 2023
‘... 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.
Ojaswi
on 3 Mar 2023
Matt J
on 3 Mar 2023
What do you mean you're "not allowed" to change the code. What would happen if you did?
Fangjun Jiang
on 3 Mar 2023
@Matt J, I like your thinking. :)
Ojaswi
on 3 Mar 2023
Voss
on 3 Mar 2023
@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).
Steven Lord
on 3 Mar 2023
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.)
Fangjun Jiang
on 3 Mar 2023
@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.
Accepted Answer
More Answers (0)
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!