Forward optional args to another function

33 ビュー (過去 30 日間)
memam
memam 2022 年 6 月 3 日
編集済み: Julian Hapke 2022 年 7 月 6 日
Hi Matlab Community,
can you help me out with the following problem:
func1(obj, arg1, options)
arguments
obj
arg1 string
options.x string = ""
options.y string = ""
end
func2(arg1, options)
end
func2(obj, arg1, options)
arguments
obj
arg1 string
options.x string = ""
options.y string = ""
end
end
% func1("a", x="b", y="c")
% Throws: Invalid argument at position 2. Function requires exactly 2 positional input(s).
How to pass a struct with optional arguments to another function?
Thanks in advance
  2 件のコメント
Rik
Rik 2022 年 6 月 3 日
I never work with the argument block, so I don't have a clear answer for you.
How would you pass a struct as an input anyway with the arguments block?
memam
memam 2022 年 6 月 7 日
I want to call func1 with defined args: func1(arg1, x="a", y="b", z="c").
When parsing the args (argument validation) in func1, I get a struct for all optional args. Then I want to call in func1 another function with the same optional arguments and only forward the argument struct to func2, without explicitly define each argument in the function call again.

サインインしてコメントする。

採用された回答

Julian Hapke
Julian Hapke 2022 年 7 月 6 日
編集済み: Julian Hapke 2022 年 7 月 6 日
there is
namedargs2cell
in the examples they use it for forwarding of name-value arguments.

その他の回答 (1 件)

Rik
Rik 2022 年 6 月 3 日
編集済み: Rik 2022 年 6 月 3 日
This is a terrible cludge. There must be a better way. If you don't find it, feel free to use this. It uses the fact that the x="b" syntax is an extension of the normal NameValue syntax, which you can still use with a comma separated list. Be careful with struct arrays.
This will still work if there are more options in the lower option, but if you want them to be forwarded, they must be defined in the upper function.
func1(1,"a", x="b", y="c", z2="e")
obj = 1
arg1 = "a"
options = struct with fields:
x: "b" y: "c" z1: "" z2: "e"
func1(1,"a", x="b", y="c", z1="d")
Error using solution>func1
Invalid argument name 'z1'. Name must be 'x', 'y', or 'z2'.
function func1(obj, arg1, options)
arguments
obj
arg1 string
options.x string = ""
options.y string = ""
options.z2
end
unwound_options=fieldnames(options);
unwound_options(:,2)=struct2cell(options);
unwound_options=reshape(unwound_options.',1,[]);
func2(obj, arg1,unwound_options{:})
end
function func2(obj, arg1, options)
arguments
obj
arg1 string
options.x string = ""
options.y string = ""
options.z1 string = ""
options.z2 string = ""
end
obj, arg1, options
end
  2 件のコメント
memam
memam 2022 年 6 月 7 日
Thank you very much for your answer Rik!
Thats the first working option, but definitely not a smooth solution and as you said a terrible cludge.
Rik
Rik 2022 年 6 月 7 日
I personally don't use the arguments block, so I don't know what the proper way would be. I personally just use my own functions to parse optional arguments. That also allows me to keep my code compatible with older releases and with GNU Octave.
If no other answer is posted with a better solution, please consider marking this one as accepted answer. While not perfect, it does work and accepting it may increase visibility for other people with a similar problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by