Is it possible to concatenate argument lists in nested factory functions?

1 次查看(过去 30 天)
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.

采纳的回答

Titus Edelhofer
Titus Edelhofer 2015-8-18
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 个评论
Josh Kirby
Josh Kirby 2015-8-18
Thanks, it looks like that's exactly what I'm looking for. I didn't realize that you could just directly concatenate lists like that.
Titus Edelhofer
Titus Edelhofer 2015-8-19
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by