Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Script is causing error

조회 수: 1 (최근 30일)
Mahdi
Mahdi 2014년 6월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (1개)

Mahdi
Mahdi 2014년 7월 18일
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!

Translated by