Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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 分配给varargout时使用可变输出数_Matlab - Fatal编程技术网

Matlab 分配给varargout时使用可变输出数

Matlab 分配给varargout时使用可变输出数,matlab,Matlab,我正在为我的classA创建一个泛型subsref,它有一个属性attrA,它是classB实例。 到目前为止,它正在发挥作用,它让我可以做类似的事情 x = objA.attr1.methB(), 这就是我一开始想要做的 function this = Class1(varargin) this.attrA = ClassB() this = class(this,'ClassA') function this = ClassB() this.AttrB1 = '

我正在为我的classA创建一个泛型subsref,它有一个属性attrA,它是classB实例。 到目前为止,它正在发挥作用,它让我可以做类似的事情

x = objA.attr1.methB(), 
这就是我一开始想要做的

function this = Class1(varargin)
    this.attrA = ClassB()
    this = class(this,'ClassA')

function this = ClassB()
    this.AttrB1 = 'valueB1'
    this.AttrB2 = 'valueB2'

function out = methB
    out = this.AttrB2
我偶然发现的问题是: 当在我的subsref中执行对方法的调用时,我会这样做(检测它是一个方法,等等,之前已经完成了):

问题是,当methName方法支持可变数量的输出时,这个varargout并不等同于[x,y,…](输出的数量应该在subsref调用中分配,因此methName总是返回单个输出,这并不总是我想要的(几乎,但不总是)

我如何让methName知道我需要多少输出?(我不想把N作为参数传递)

我想创建一个字符串
str='[out1,out2,out3…]'
然后做一些类似的事情

eval([ 
str ...
'= {feval(methName,this,args{:})};'...
])

但是我一直在想,一定有一种更优雅的方法可以做到这一点。

如果您期望的输出数量取决于从subsref请求的输出参数,您可以简单地使用
nargout
,如下所示:

[varargout{1:nargout}] = feval(methName,this,args{:});

这个解决方案有效,但我需要为单个输出添加一些内容:

if iscell(args)
    [argout{:}] = feval(methName,this,args{:});            
else
    [argout{:}]= {feval(methName,this,args)};
end %end if iscell`
if isSingleCell(argout) && iscell(argout{1})`
    v = argout{1};
    argout{1}=v{1};
end

我不确定,但可能最后一位只是修复其他东西所必需的(我必须修复很多其他东西才能使这个工作正常)。当我完成我试图对这个类所做的事情时,我会回到它上

如何
[output{1:n}={feval(methName,this,args{:}}
n
是您期望的输出数?我希望我不是唯一一个在阅读
methName
时在屏幕后面有点傻笑的人。感谢这篇文章,它对我很有帮助。应该标记解决方案
if iscell(args)
    [argout{:}] = feval(methName,this,args{:});            
else
    [argout{:}]= {feval(methName,this,args)};
end %end if iscell`
if isSingleCell(argout) && iscell(argout{1})`
    v = argout{1};
    argout{1}=v{1};
end