MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   将匿名函数定义为m文件函数的4个输出中的2个 (https://www.labfans.com/bbs/showthread.php?t=23883)

poster 2019-12-10 20:48

将匿名函数定义为m文件函数的4个输出中的2个
 
我有一个带有4个输出的m文件功能。我想定义一个具有相同输入但只产生四个输出中的两个的匿名函数。这可能吗?



[B]回答:[/B]

如果两个输出分别是#1和#2,那么一切都很好,您不必担心其他两个输出。

如果两个输出是其他两个,则有两个选择

(1)创建一个具有两个输出的包装函数(请注意,在较新版本的Matlab中,您可以将~替换未使用的输出dummy 。

function [out1,out2] = wrapperFunction(in1,in2,in3) [dummy,out1,dummy,out2] = mainFunction(in1,in2,in3); (2)添加另一个输入变量,该变量允许您切换函数的行为

function varargout = mainFunction(in1,in2,in3,outputSwitch) %# make output switch optional if nargin < 4 || isempty(outputSwitch) outputSwitch = 0; end %# calculation here that creates out1-4 if outputSwitch %# the special case where we only want outputs 2 and 4 varargout = {out2,out4}; else %# return all four outputs varargout = {out1,out2,out3,out4} end 然后,您可以照常创建匿名函数。



[url=https://stackoverflow.com/questions/3673392]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:21

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.