Pregunta

I run a lot of function on a series of files. so it's logical to make a function that runs my functions on files instead repeating the procedure in every file. The problem is number of arguments for every function is different! So the psudo-code is:

function [out1, out2]=batchDo(@func,adrs,arg1,arg2,...,argn)

files=ls(adrs);
for i=1:length(files)
    raw=load([adrs files(i)]);
    [out1, out2]=func(raw,arg1,arg2,...,argn)
    out1s=out1+out1s;
    out2s=out2+out2s;
end
out1=out1s/length(files);
out2=out2s/length(files);

Fortunately in my case outputs are almost similar and I can add a few dummies to make them all similar. But if there is anyway to address them similarly, I would be grateful.

¿Fue útil?

Solución 2

Horchler answer is the right one, if I just pass the varargin as varargin{:} that automatically provides what I need. If you use the varargin in every function you have and then trying to address varargin.arg1 instead of arg1, it slows down your code. There are numerous articles that compare the performances.

Otros consejos

If the inputs are in some order (or can be re-ordered) such that, for example:

  • all functions take arguments 1 through 3
  • functions that take additional arguments take them in order: arg4 through arg8, arg4 through arg10, etc.

Then you can do this quite simply by adding varargin to each function that doesn't use the full set of arguments, but only in place of the unused arguments (as if defining optional arguments), and then ignoring it within the actual function. So a function might be:

function myfunction(raw, arg1, arg2, arg3, arg4, arg5, varargin)

Where in fact this only uses arg1...arg5 and the others are ignored.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top