How to neatly take user input as function input
14 views (last 30 days)
Show older comments
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.
Accepted Answer
Walter Roberson
on 28 Feb 2019
Edited: Walter Roberson
on 2 Mar 2019
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'});
More Answers (0)
See Also
Categories
Find more on Function Creation 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!