在Matlab默认参数 [英] Default Arguments in Matlab
问题描述
时有可能有默认参数在Matlab?举例来说,在这里:
Is it possible to have default arguments in Matlab? For instance, here:
function wave(a,b,n,k,T,f,flag,fTrue=inline('0'))
我想有真正的解决方案是一个可选的参数波函数。如果可能的话,任何人都可以证明正确的方式做到这一点?目前,我想我上面张贴,我得到:
I would like to have the true solution be an optional argument to the wave function. If it is possible, can anyone demonstrate the proper way to do this? Currently, I am trying what I posted above and I get:
??? Error: File: wave.m Line: 1 Column: 37
The expression to the left of the equals sign is not a valid target for an assignment.
谢谢!
推荐答案
据我所知,没有一个直接的方式来做到这一点,就像你尝试。
As far as I know, there isn't a direct way to do this like you've attempted.
通常的方法是使用可变参数和对证args来数。是这样的:
The usual approach is to use varargs and check against the number of args. Something like:
function f(arg1,arg2,arg3)
if nargin < 3
arg3 = 'some default'
end
end
有几个票友东西,你可以用的isEmpty等做的,你可能想看看MATLAB中心为一些软件包捆绑这些事情。
There are a few fancier things you can do with isempty, etc., and you might want to look at matlab central for some packages that bundle these sorts of things.
[更新]
高兴的是帮助。
[update] glad that helped.
您可以看看varargin,nargchk等,他们对这种事有用的功能。可变参数让你离开可变数量的最终参数,但是,这并不让你周围的一些/所有这些默认值的问题。
you might have a look at varargin, nargchk, etc. they're useful functions for this sort of thing. varargs allow you to leave a variable number of final arguments, but this doesn't get you around the problem of default values for some/all of them.
这篇关于在Matlab默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!