How to neatly take user input as function input

14 views (last 30 days)
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 Comments
Joe_Z
Joe_Z on 2 Mar 2019
Hi Bob,
Thanks for your response; that's great just tried this and it worked. Before I tried to call inputdlg to set the value of an input within the function script itself, on the second line. But obviously in doing that, I had to have the varaible already defined in the main script to input into the function in the first place. Anyways sorted now, cheers!

Sign in to comment.

Accepted Answer

Walter Roberson
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'});
  3 Comments
Joe_Z
Joe_Z on 18 Mar 2019
Hi Walter, apologies for the delay in response. Thanks so much for this thats really useful!

Sign in to comment.

More Answers (0)

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!