1

I have a function which accepts a variable number of input variables. The problem is, the number of input arguments I'm going to provide varies. As such, I store all the arguments in a structure:

function grandAvg(datafiles)

% Load up all averaged datafiles
avgs = struct();
for n=1:length(datafiles)
    avgs(n).avg = load(datafiles{n});
end

My question is, is there a way to expand this argument for the function? I need a way to convert this:

% DOES NOT WORK
avg = ft_timelockgrandaverage(cfg, avgs);

to this:

% WOULD WORK, BUT DO NOT WANT TO TYPE IT OUT
avg = ft_timelockgrandaverage(cfg, avgs(1).avg, ..., avgs(n).avg); 

EDIT TO ADD: So apparently my question wasn't clear. I know how to construct the function using varargin. My question was, if I am trying to use a build-in function which I don't want to or can't modify, how can I provide arguments in a variable manner? I.e., I don't know ahead of time how many argument's I'll be providing, so when I call the function, I'll have to call it with X number of arguments. In effect, I'm looking for a way to turn this:

someVar  <1xN struct>

into this:

someVar1  <1x1 struct>
someVar2  <1x1 struct>
...
someVarN  <1x1 struct>

in a programmatic manner. Thanks!

4

4 回答 4

4

An alternative to using a structure array to store your data would be to use a cell array. For example:

nFiles = numel(datafiles);  %# Number of files
avgs = cell(1,nFiles);      %# Initialize an empty cell array
for iFile = 1:nFiles        %# Loop over the files
  avgs{iFile} = load(datafiles{iFile});  %# Load the data into each cell
end
avg = ft_timelockgrandaverage(cfg, avgs{:});  %# Pass the contents to a function

The syntax avgs{:} dumps the contents of the cell array into what's called a comma-separated list. It is equivalent to typing avgs{1}, avgs{2}, ... , avgs{end}. The syntax foo(:).bar from the answer you found also creates a comma-separated list, but I find that using cell arrays for such a purpose is generally cleaner than using a structure array.

于 2011-07-27T14:05:01.427 回答
3

yes you can use variable length input argument list varargin

http://www.mathworks.com/help/techdoc/ref/varargin.html

于 2011-07-26T21:35:43.000 回答
2

So, after playing around, I've got it. Using the example from above:

Given an 1xN struct named foo, where each foo(n) contains the field bar, I would call the function as:

function(foo(:).bar);

This is the same as typing

function(foo(1).bar, foo(2).bar, ..., foo(N).bar);

In this way, I can dynamically expand or shrink foo and still have no problem calling the function.

于 2011-07-27T13:28:07.747 回答
0

You can surely do such a thing, by means of the varargin construct in MATLAB. This will be something like:

avg = ft_timelockgrandaverage(cfg, avgs.avg); 

And for the function ft_timelockgrandaverage

function output = ft_timelockgrandaverage(config, varargin)
% your code here

varargin will be a cell array: {avgs(1).avg, avgs(2).avg, ..., avgs(3).avg} which you can process.

于 2011-07-26T22:32:16.867 回答