我有一段代码在几个例程中重复,但是我不能创建它的功能,因为它应该更改输入,这是varargin。因此,解决方案似乎将这段代码放在一个m文件中,并在调用例程中提及该名称。当我将代码移动到一个文件并调用它时,所有变量将与它是代码体的方式完全相同,但是在脚本返回错误之后出现的调用函数:“太多输出参数”当我把脚本放在正文中,这个错误不再显示!!!
代码是:
function varargout=batchMean(@func,adrs,varargin)
% code I want to move to script file
grd=find(strcmp(varargin,'grid'));
grdfl=find(strcmp(varargin,'gridFile'));
if grd
% If grid is provided in the arguments
grid=varargin{grd+1};
varargin(grd:grd+1)=[];
elseif grdfl
grdFile=cellstr(ls([adrs varargin{grdfl+1} '*']));
varargin(grdfl:grdfl+1)=[];
if isempty(grdFile)
error('Hall_lab:batchAV:argin', ...
'Grid file mentioned in input could not be found');
end
[grid,err]=matRead([adrs grdFile{1}]);
if err
error('Hall_lab:batchAV:grid', ...
'Could not read grid file');
end
else
% Reading grid file
grdFile=cellstr(ls([adrs '*Grd.mat']));
if isempty(grdFile)
grdFile=cellstr(ls([adrs '*grd.mat']));
end
if length(grdFile)>1
error('Hall_lab:batchAV:fileSet', ...
'There is more than one file for grid');
elseif isempty(grdFile{:});
error('Hall_lab:batchAV:fileSet', ...
'There is no file for grid');
end
[grid,err]=matRead([adrs grdFile{:}]);
if err
error('Hall_lab:batchAV:grid', ...
'Could not read grid file');
end
files=setdiff(files,grdFile);
end
% end of code I want to move to script file
% calling the function
[varargout{1:outNums}]=func(grid,data,inf,varargin{:});
% actual function being called
function [ave]=meanPIV(~,data,~)
ave=mean(data,ndims(data));
end
答案 0 :(得分:0)
似乎batchmeans没有输出。您正在使用varargout,但您从未设置它(请参阅此处的示例http://www.mathworks.com/help/matlab/ref/varargout.html)。为什么要修改varargin?也许你的功能应该只是
[grid,err] = batchMean(func,adrs,varargin)