Info

This question is closed. Reopen it to edit or answer.

Script is causing error

2 views (last 30 days)
Mahdi
Mahdi on 16 Jun 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a piece of code that is being repeated in several routines, however I cannot make a function of it since it should changes inputs which is varargin. So the solution seems to put the piece of code in a m file and mention the name in calling routines. When I move the code to a file and call it, all the variables will be exactly the same way as if it is the body of code, however a calling function that comes after the script returns error: "Too many output arguments" when I put the script in the body, this error is not showing up anymore!!!
the code is:
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
[out{1:outNums}]=func(grid,data,inf,varargin{:});
% actuall function being called
function [ave]=meanPIV(~,data,~)
ave=mean(data,ndims(data));
end

Answers (1)

Mahdi
Mahdi on 18 Jul 2014
I'm struggling with this for a long time, can't anyone help please?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!