How to neatly take user input as function input

11 visualizzazioni (ultimi 30 giorni)
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 Commenti
Bob Thompson
Bob Thompson il 28 Feb 2019
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?
Joe_Z
Joe_Z il 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!

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 28 Feb 2019
Modificato: Walter Roberson il 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 Commenti
Walter Roberson
Walter Roberson il 2 Mar 2019
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.
Joe_Z
Joe_Z il 18 Mar 2019
Hi Walter, apologies for the delay in response. Thanks so much for this thats really useful!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Schedule Model Components in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by