'Can I save a function's arguments in Matlab to reuse in another function?

I am using the topoplot function in Matlab as follows:

topoplot(DATA, channels, ...
    'maplimits',  topoYlim, 'electrodes', 'on', 'emarker', {'.','k',[6],1}, ...
    'whitebk', 'on', 'style', 'both', 'shading', 'interp', 'drawaxis', 'off', ...
    'numcontour', [0]);

The problem is that I have to use this function several times in different parts of the script. That really makes the script unnecessarily bulky and difficult to read.

Is there a way to save all the arguments in a variable (like a structure) so that I can just call something like:

topoplot(DATA, options)

P.S. I have tried something like:

options = splitvars(table(NaN(1,6)));
options.Var1_1 = 'maplimits'
options.Var1_2 = [-1 1]
options.Var1_3 = 'electrodes'
options.Var1_4 = 'on'
options.Var1_5 = 'emarker'
options.Var1_6 = {'.','k',[6],1}

but it doesn't work.

Thank you in advance

This is my suboptimal solution. I have created a separate function as follows:

    function makeTopoplot(dataToPlot, channelsToUse, topoYlim)
             topoplot(dataToPlot, channelsToUse, 'maplimits', topoYlim,
             'electrodes', 'on', 'emarker', {'.','k',[6],1}, ...
             'whitebk', 'on', 'style', 'both', 'shading', 'interp',
             'drawaxis', 'off', 'numcontour', [0]);
    end

Again, I would have preferred something without creating an external function, but didn't manage yet...



Solution 1:[1]

Other than @Cris's suggestion (which works perfectly of course), another option is to make a local function in your script file that has all the options. Your script file then looks like this:

data1 = ...;
myTopoplot(data1, channels);
data2 = ...;
myTopoplot(data2, channels);

% Wrapper around TOPOPLOT to set up common arguments
function myTopoplot(data, channels)
topoYlim = ...;
topoplot(data, channels, ...
    'maplimits',  topoYlim, 'electrodes', 'on', 'emarker', {'.','k',[6],1}, ...
    'whitebk', 'on', 'style', 'both', 'shading', 'interp', 'drawaxis', 'off', ...
    'numcontour', [0]);
end

In some cases this can be easier to follow than having a cell array of common options.

Solution 2:[2]

You can store the arguments in a cell array as follows:

arguments = {
    'maplimits',  topoYlim, 'electrodes', 'on', 'emarker', {'.','k',[6],1}, ...
    'whitebk', 'on', 'style', 'both', 'shading', 'interp', 'drawaxis', 'off', ...
    'numcontour', [0]
};

And then use them in the function call as follows:

topoplot(DATA, channels, arguments{:});

arguments{:} generates a comma-separated list with all the contents of the cell array. It is the same as typing arguments{1}, arguments{2}, arguments{3}, ....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Edric
Solution 2 Cris Luengo