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에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by