Borrar filtros
Borrar filtros

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

23 visualizaciones (últimos 30 días)
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 comentarios
Steven Lord
Steven Lord el 3 de Mzo. de 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
Fangjun Jiang el 3 de Mzo. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Mzo. de 2023
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 comentario
Ojaswi
Ojaswi el 3 de Mzo. de 2023
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by