Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matlab中用两种不同的输入方式编写函数_Matlab_Input - Fatal编程技术网

在Matlab中用两种不同的输入方式编写函数

在Matlab中用两种不同的输入方式编写函数,matlab,input,Matlab,Input,我想知道如何用两种不同的方法在Matlab中编写一个函数来传递相同的输入 其中一个例子是内置函数lsqcurvefit。您可以这样传递输入: x=lsqcurvefit(fun、x0、扩展数据、ydata、lb、ub、选项) 其中前4个参数是必需的,后3个是可选的,或者您可以这样称呼它: x=lsqcurvefit(问题) 其中problem是一个包含相同输入的struct。在这种情况下,只需要一个输入 我的问题是:如何编写一个可以以两种不同方式接受相同输入的函数 我看了文档,似乎找不到它,因为

我想知道如何用两种不同的方法在Matlab中编写一个函数来传递相同的输入

其中一个例子是内置函数
lsqcurvefit
。您可以这样传递输入:

x=lsqcurvefit(fun、x0、扩展数据、ydata、lb、ub、选项)

其中前4个参数是必需的,后3个是可选的,或者您可以这样称呼它:

x=lsqcurvefit(问题)

其中
problem
是一个包含相同输入的
struct
。在这种情况下,只需要一个输入

我的问题是:如何编写一个可以以两种不同方式接受相同输入的函数

我看了文档,似乎找不到它,因为我不知道要搜索什么词或术语。。。有人能给我指一下这个吗?我相信这很简单。也许它很琐碎,而我只是错过了它


编辑:似乎我一直在寻找的是“函数重载”和“函数签名”

我不确定如何在matlab上编写代码,但我相信所有编程语言的概念都是一样的,所以“只要做方法重写,其中一个方法将传递4个参数,另一个传递2个参数。 e、 g


vargin
nargin
是关键词。@天哪,好吧,让我们来推广这个想法只是为了好玩。如果我想以两种不同的方式传递输入,但这两种方式都涉及传递具有不同内部结构的单个
struct
。然后nargin会告诉我有一个输入,我将无法区分类型。是吗有什么办法吗?我觉得nargin和varargin通常是一种很差的改变函数行为的方法。我不需要依靠输入的数量来改变行为。在下面的回复之后,我现在知道我在寻找更类似于“函数重载”的东西。更多信息请参阅本文:可能的重复请参阅:
//assume that the  following method are defined within the class

public void FirstFunction( int x, int y,int c,int f){
    System.out.print(x + y + c + f);
}

public void SecondFunction( int m,int s){
    System.out.print(m - s);
}

//within the main method on java you call these method
//so it will depend on the number of parameters you have passed the it will call.