필터 지우기
필터 지우기

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

조회 수: 18 (최근 30일)
Ojaswi
Ojaswi 2023년 3월 3일
댓글: Ojaswi 2023년 3월 3일
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
Steven Lord
Steven Lord 2023년 3월 3일
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 2023년 3월 3일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 3일
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
Ojaswi
Ojaswi 2023년 3월 3일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by