How to neatly take user input as function input
조회 수: 19 (최근 30일)
이전 댓글 표시
I have a function in which I would like the user to input an argument. Currently I have:
%take user input and set to var 'spike_threshold'
spike_threshold = inputdlg('Choose a firing threshold in Vm (default, 20)');
spike_threshold = str2num(spike_threshold{1,1});
if isempty(spike_threshold);
spike_threshold = 10;
end
%use 'spike_threshold' as an input argument to a function
[spike_counts, Cell_info.spikeinfo] = spike_extractor(Vm, time, current_injections_norm_vector, spike_threshold);
This works fine but takes up a lot of space, I have a few functions where I would like the user to input 2-3 arguments. Is there a way to have this process embdeded within the function? I tried this but it required the input variable to be defined in the script the function was called from. This would be better, but still a little messier than I'd like - is it possible to have this process entirely self-contained within the function? Cheers.
댓글 수: 2
Bob Thompson
2019년 2월 28일
is it possible to have this process entirely self-contained within the function?
I'm a bit confused. Are you asking if it is possible to insert an input command into a function directly? Yes, and it works the same way as in a script.
I tried this but it required the input variable to be defined in the script the function was called from
You tried what, exactly? It sounds like you tried to use the 'input' command within a function, which should work fine. If you did try putting the input command into your functions, what errors did you encounter, or what results did you get, and how were they different from what you were expecting?
채택된 답변
Walter Roberson
2019년 2월 28일
편집: Walter Roberson
2019년 3월 2일
function varargout = ask_and_run(f, input_prompts, defaults)
input_values_cell = questdlg(input_prompt, defaults);
input_values_num = cellfun(@str2double, input_values_cell, 'uniform', 0);
varargout{1:nargout} = f(input_values_num{:});
end
such as
[spike_counts, Cell_info.spikeinfo] = ask_and_run(@(time, st) spike_extractor(VM, time, current_injections_norm_vector, st), {'Choose a time', 'Choose a firing threshold in Vm'}, {'1', '10'});
댓글 수: 3
Walter Roberson
2019년 3월 2일
The @ I had in the first line was a typo; I have corrected it.
My (probably wrong) understanding is that in line 1 of code section 1, 'f' is set as a function handle, and once the input values are taken by the user and converted to double, 'f' is used to recall the function to set the output.
Small correction that f is expected to be passed as a function handle (probably to an anonymous function) but otherwise Yes.
In the second code section, time and st are set as function handles and when used as inputs to spike_extractor, these call the ask_and_run function to determine their value?
No, the code fragment
@(time, st) spike_extractor(VM, time, current_injections_norm_vector, st)
creates an anonymous function that expects two inputs. Copies of the values of VM and current_injections_norm_vector will be taken at the time the function handle is created and will be saved along with the function handle. Any changes to VM or current_injections_norm_vector after that will be ignored by the function handle.
At the time the function is invoked with two arguments, the code
spike_extractor(VM, time, current_injections_norm_vector, st)
will be executed, recalling the saved values of VM and current_injections_norm_vector, and substituting the first passed parameter where time is and substituting the second passed parameter where st is. But that does not happen until the function is invoked.
Meanwhile back at the call to ask_and_run, {'Choose a time', 'Choose a firing threshold in Vm'} will be passed as the second parameter to the function (with the first parameter being the function handle), and {'1', '10'} will be passed as the third parameter.
Once the arguments are constructed, the function ask_and_run will be invoked. The code will take the second passed parameter and the third passed parameters and pass them to the inputdlg() function for display of questions and setting of default values. inputdlg() will put its graphics up on the screen and wait for a response. When the user clicks okay, inputdlg() will return a cell array of character vectors of the user responses.
The user responses are then converted to numeric form by calling str2double on each cell entry, giving a cell output.
The members of the cell are then passed to the function handle that was provided, as separate arguments.
Note: in its current form, this code expects the user to give exactly one numeric input for each prompt. If non-numeric values had to be accepted, or if non-scalar numeric values had to be accepted, the cellfun(@str2double) would have to be modified.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Schedule Model Components에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!