我如何通过一系列的参数数目未知的在MATLAB的函数吗? [英] How can I pass a set of an unknown number of arguments to a function in MATLAB?
问题描述
当你有一个函数,变量参数号(如ndgrid),你怎么可以传递参数的任意列表,该功能?
when you have a function that takes a variable argument number (like ndgrid), how can you pass an arbitrary list of arguments to that function?
例如我想使它这样有时我传递两个向量ndgrid,走出两个矩阵,即
For example I want to make it so that sometimes I pass two vectors to ndgrid and get out two matrices, i.e.,
[X1,X2] = ndgrid(x1,x2);
但其他时候,我可能会有更多X的,所以我会想
But other times I might have more X's, so I'll want
[X1,X2,X3,X4] = ndgrid(x1,x2,x3,x4)
有什么样的结构,我可以用它来存储一个未知数量的参数列表,然后只给该列表功能?而且是有没有办法从一个函数检索所有的输出,当你不知道有多少会出现?
Is there any kind of structure I can use to store a list of an unknown number of arguments and then just give that list to a function? And is there a way to retrieve all of the outputs from a function, when you don't know how many there will be?
感谢。
推荐答案
使用 varargin
, nargin
, varargout
和 nargout
您可以轻松定义变量参数/输出功能。见附件MATLAB文档链接,varargin页面。其他人都在底部链接:
Using varargin
,nargin
, varargout
and nargout
you can easily define variable argument/output functions. See the attached MATLAB documentation link for the varargin page. The others are linked at the bottom:
<一个href=\"http://www.mathworks.com/help/matlab/ref/varargin.html\">http://www.mathworks.com/help/matlab/ref/varargin.html
编辑:BTW,不要嘟嘟我自己的号角,但它似乎被实现,就像我在快速和肮脏的注释呵呵呵曾建议
BTW, not to toot my own horn, but it seems to be implemented just as I had suggested in the "quick-and-dirty" comment hehehe
这篇关于我如何通过一系列的参数数目未知的在MATLAB的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!