Is it possible to concatenate argument lists in nested factory functions?
1 view (last 30 days)
Show older comments
Josh Kirby
on 18 Aug 2015
Commented: Titus Edelhofer
on 19 Aug 2015
I'm currently working on a simulation that involves a 2D field that varies across space and time. I'm going to be needing to test a variety of such fields, so I'm using a factory to generate the field function, taking in some configuration parameters and the current time and returning an anonymous function that takes two inputs (x,y) and returns a single field value.
That works fine if I hardcode in what the factory and configuration parameters are going to be. However, I'd like to be able to change the configuration parameters and even the field factory between runs, and if I can I'd like to avoid passing those parameters around, building them into a higher-level factory function that locks the non-time parameters in place while leaving the current time to be established as needed during the run. This is somewhat complicated by the fact that the low-level factory has an unknown number of input arguments, though I can be sure that the current time will be last (or maybe first, I don't think that overly matters).
My initial thought is to make something like this:
function [ fixedargumentfactory ] = MetaFieldFactory( variableargumentfactory, inputarguments )
fixedargumentfactory = @(curtime) variableargumentfactory([inputarguments,curtime]{:});
end
but that violates Matlab's rules on indexing.
Is there any way to get this sort of design to work in Matlab, or will I need to rework something somewhere?
For now, I'll see if I can work around the issue by changing the lower-level factories to have exactly two input parameters, one of which is an array of all of the configuration parameters. That way I won't have to concatenate arguments, just feed in inputarguments and curtime separately.
0 Comments
Accepted Answer
Titus Edelhofer
on 18 Aug 2015
Hi,
I'm not 100% sure about what you try to achieve, but solely looking on the indexing, I think it should look more like
function [ fixedargumentfactory ] = MetaFieldFactory( variableargumentfactory, inputarguments )
fixedargumentfactory = @(curtime) variableargumentfactory(inputarguments{:}, curtime{:});
end
Titus
2 Comments
Titus Edelhofer
on 19 Aug 2015
I'm glad to hear. For a cell array A writing A{:} is equivalent to writing A{1}, A{2}, ..., A{end}. So
fcn(A{:},B{:})
is interpreted by MATLAB as
fcn(A{1},A{2},...,A{end},B{1},B{2},...,B{end})
Titus
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!