简体   繁体   English

在Matlab中,对于多输入函数,如何将单个输入用作多个输入?

[英]In Matlab, for a multiple input function, how to use a single input as multiple inputs?

I have a function that takes a variable number of inputs, say myfun(x1,x2,x3,...) . 我有一个函数,它接受可变数量的输入,比如myfun(x1,x2,x3,...)

Now if I have the inputs stored in a structure array S, I want to do something like myfun(S.x1,S.x2,...) . 现在如果我将输入存储在结构数组S中,我想做像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. 您可以先使用STRUCT2CELL将结构转换为单元格数组,然后使用它来生成多个输入的列表。

S = struct('x1','something','x2','something else');
C = struct2cell(S);
myfun(C{:});

Note that the order in which the fields in S are defined are the order in which the inputs are passed. 请注意,定义S中字段的顺序是输入的传递顺序。 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 . 要检查字段的顺序是否正确,可以在S上运行fieldnames ,该字段名返回一个字段名称与C的值对应的单元格。

Something to add to Jonas' answer: Actually you can omit the struct and go right for the cell which is then expanded into a list for the function arguments: 要添加到Jonas的答案中的东西:实际上你可以省略结构并向右移动单元格,然后将其扩展为函数参数的列表:

c = {125, 3};
nthroot(c{:})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在matlab中使用向量作为多个输入函数的输入 - Using a vector as an input for a function of multiple inputs in matlab Matlab:将多输入匿名函数转换为单输入 - Matlab: Turning a multiple input anonymous function into a single input MATLAB:如何在输入向量的所有可能组合上评估具有多个输入的函数 - MATLAB: How to evaluate a function with multiple inputs on all possible combinations of input vectors 如何反转具有多个输入的 function 的估计值,但仅反转单个输入的 function - How to invert the estimate of a function with multiple inputs, but only invert the function for a single input MATLAB的“ if”函数如何处理多个输入 - How MATLAB's “if” function handles multiple inputs 如何使用输入 arguments 运行多个 matlab function 并具有相似的名称 - How to run multiple matlab function with input arguments and have similar names MATLAB:如何对多个输入使用fsolve,每个输入都取决于多个参数? - MATLAB : How to use fsolve with multiple inputs, each dependent on multiple parameters? 使用单个矩阵作为函数的多个输入参数 - Using a single matrix as multiple input arguments to a function 如何为Matlab功能块创建多个输入? - How can I create multiple inputs for a matlab function block? 如何在Matlab中将多个输入应用于函数句柄数组? - How to apply multiple inputs to an array of function handles in Matlab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM