Let’s say I have func1, func2 and func3. Is there any way to call them with a known argument having defined their names? (Or their pointers? handlers?)
Something like:
toBeRunned = [ 'func1'; 'func2'; 'func3' ];
// .. foreach entry of toBeRunned call the function with VAR params ..
This is what function handles are meant for.
A little more explanation on what we’re doing here:
toBeRun is a cell array of function handles, just an arbitrary list. For a function written as an M-file, added the
@is all the is required to create a function handle.In order to evaluate the function, it needs to be removed from the cell array (into
fnTmpin this case.) This is a syntax limitation of Matlab. You can also usefneval, but I usually do it this way.Any arguments can be added as needed, in the usual way (e.g.
fnTmp(1,2,3,'four')). But if you need this much abstraction, you may also need to use an arbitrary number of input arguments. ThefnArgumentscell is a pretty flexible way of accomplishing this.