nokturn
nokturn

Reputation: 11

Passing names of functions to another function with parameters as input in MATLAB

Problem Statement: I am trying to write MATLAB code for a main caller function (like run_experiment below) to specify which computations I want to execute where computations are made sequentially using other MATLAB functions. Those other functions are to be evaluated based on parameters passed with the main caller function. The said functions used in computations are to be specified with name of the scripts they are written in.

Example Desired Code Behavior: For example, a command like the following should run the preprocess_data, initialise_model and train_model scripts.

>> run_experiment('dataset_1_options', '|preprocess_data|initialise_model|train_model|');

And this command should run only the train_model script but also evaluates its performance:

>> run_experiment('dataset_1_options', '|train_model|evaluate_model|');

In the above examples "|" is used as a delimiter to specify separate function names to be evaluated. Those functions use the options specified with dataset_1_options. Please do not focus on how to separate that part of the input into meaningful function names; I know how to do it with strsplit.

Constraints and Specifications: The function names to be passed as input to the main caller function are NOT anonymous functions. The purpose is to be able to pass such multiple function names as input AND to evaluate them with the options like the above example. They return output to be evaluated in other parts of the research code (i.e. passing data matrices to other functions within the research code as results of the computations carried out within them.)

Question: Given the desired behavior and constraints mentioned above, can anybody help in how to pass the separate function names from another caller function along with options/parameter to those functions? How should the main caller function evaluate the function names passed in as input with the options specified during the call?

Thank you in advance.

Upvotes: 1

Views: 195

Answers (2)

ammportal
ammportal

Reputation: 991

What you need to do this is create a cell array with your function names and another array with the corresponding options as below

% Function name array
fn_array = {@fn_1, @fn_2, ...};
% Option array
option_array = {{fn1_opt1, fn2opt2, ...}; {fn2_opt1, fn2_opt2, ...};, ...};

These two need to be passed to your run_experiment function which will evaluate them as below

function run_experiment(fn_array, option_array)
num_fn = length(fn_array); %Finds number of functions to evaluate
for ii = 1:num_fn %Evaluates each function
    fn_array{ii}(option_array{ii}{:});
end

Upvotes: 1

debo
debo

Reputation: 380

You can pass functions to functions in matlab. You just need to use the @ sign when you pass it. In your case it would be run_experiment('dataset_1_options', @train_model) inside a script. You could keep your options in a cell array or something. The run_experiment function would just be a regular function,

function [output] = run_experiment(options, train_model, ...);
train_model(options{1}, ...)
.
.
.
end

Upvotes: 1

Related Questions