在Matlab中,对于多输入功能,如何将单个输入用作多个输入? [英] In Matlab, for a multiple input function, how to use a single input as multiple inputs?
问题描述
我有一个函数,它接受可变数量的输入,例如myfun(x1,x2,x3,...)
.
I have a function that takes a variable number of inputs, say myfun(x1,x2,x3,...)
.
现在,如果我将输入存储在结构数组S中,我想做类似的事情
myfun(S.x1,S.x2,...)
.我该怎么做?
Now if I have the inputs stored in a structure array S, I want to do something like
myfun(S.x1,S.x2,...)
. How do I do this?
推荐答案
You can first convert your structure to a cell array using STRUCT2CELL, and then use that to generate the list of multiple inputs.
S = struct('x1','something','x2','something else');
C = struct2cell(S);
myfun(C{:});
请注意,在S
中定义字段的顺序是传递输入的顺序.要检查字段的顺序是否正确,可以在S
上运行fieldnames
,这将返回一个字段名称与C
中的值相对应的单元格.
Note that the order in which the fields in S
are defined are the order in which the inputs are passed. To check that the fields are in the proper order, you can run fieldnames
on S
, which returns a cell with field names corresponding to the values in C
.
这篇关于在Matlab中,对于多输入功能,如何将单个输入用作多个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!