如何优雅地忽略MATLAB函数的一些返回值? [英] How to elegantly ignore some return values of a MATLAB function?
问题描述
是否有可能从函数中获得'nth'返回值,而不必为之前的所有 n-1
返回值创建虚拟变量?
比方说,我在MATLAB中有以下函数:
pre $函数[a, b,c,d] = func()
a = 1;
b = 2;
c = 3;
d = 4;
现在假设,我只对第三个返回值感兴趣。这可以通过创建一个虚拟变量来实现:
[dummy,dummy,variableThatIWillUse,dummy] = func;
清除虚拟;
但我认为这很丑陋。我认为你可以做以下事情之一,但是你不能:
[_,_, variableThatIWillUse,_] = func;
[,,variableThatIWillUse,] = func;
variableThatIWillUse = func(3);
variableThatIWillUse = func()(3);
有没有优雅的做这个工作吗?
到目前为止,最好的解决方案是简单地使用 variableThatIWillUse
作为虚拟变量。这使我不必创建一个真正的虚拟变量来污染工作空间(或者我需要清除)。简而言之:解决方案是对每个返回值使用 variableThatIWillUse
,直到有趣的一个。返回值可以简单地忽略:
[variableThatIWillUse,variableThatIWillUse,variableThatIWillUse] = func;
我仍然认为这是非常难看的代码,但如果没有更好的方法,那么我猜我'b $ b
这有点破解,但它有效:
首先是一个简单的示例函数:
Func3 = @()deal(1,2,3);
[a,b,c] = Func3();
%产生a = 1,b = 2,c = 3
如果您在多表达式赋值的左侧使用两次变量,则较早的赋值将被后面的赋值破坏:
并[b,b,C] = FUNC3();
%yield b = 2,c = 3
[c,c,c] = Func3();
%yield c = 3
(编辑:只是为了检查,我也证实了这一点技巧适用于 [mu,mu,mu] = polyfit(x,y,n)
如果您只关心 polyfit $ c $> c>是第三个参数)
编辑:有更好的方法;请参阅 ManWithSleeve的回答。
Is it possible to get the 'nth' return value from a function without having to create dummy variables for all n-1
return values before it?
Let's say, I have the following function in MATLAB:
function [a,b,c,d] = func()
a = 1;
b = 2;
c = 3;
d = 4;
Now suppose, I'm only interested in the third return value. This can be accomplished by creating one dummy variable:
[dummy, dummy, variableThatIWillUse, dummy] = func;
clear dummy;
But I think this is kind of ugly. I would think that you might be able to do something like one of the following things, but you can't:
[_, _, variableThatIWillUse, _] = func;
[, , variableThatIWillUse, ] = func;
variableThatIWillUse = func(3);
variableThatIWillUse = func()(3);
Are there any elegant ways to do this that do work?
So far, the best solution is to simply use the variableThatIWillUse
as a dummy variable. This saves me from having to create a real dummy variable that pollutes the work-space (or that I would need to clear). In short: the solution is to use the variableThatIWillUse
for every return value up until the interesting one. Return values after can simply be ignored:
[variableThatIWillUse, variableThatIWillUse, variableThatIWillUse] = func;
I still think this is very ugly code, but if there is no better way, then I guess I'll accept the answer.
This is somewhat of a hack but it works:
First a quick example function:
Func3 = @() deal(1,2,3);
[a,b,c]=Func3();
% yields a=1, b=2, c=3
Now the key here is that if you use an variable twice in the left hand side of a multiple-expression assignment, an earlier assignment is clobbered by the later assignment:
[b,b,c]=Func3();
% yields b=2, c=3
[c,c,c]=Func3();
% yields c=3
(edit: just to check, I also verified that this technique works with [mu,mu,mu]=polyfit(x,y,n)
if all you care about from polyfit
is the 3rd argument)
edit: there's a better approach; see ManWithSleeve's answer instead.
这篇关于如何优雅地忽略MATLAB函数的一些返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!