function doesnt works well

조회 수: 3 (최근 30일)
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021년 12월 20일
댓글: Nurul Ain Basirah Zakaria 2021년 12월 21일
Hi, I am using this function;
function [s,tint] = spei(t,prec,pevap,varargin)
But, I am not sure what is varargin works for?
The size of my file are;
t=1x1x420
prec=98x56x420
pevap=98x56x420
Error using spei (line 40)
Expected a string scalar or character vector for the parameter name.
  댓글 수: 2
Star Strider
Star Strider 2021년 12월 20일
It will be necessary to carefully read the documentation for the file to understand what the arguments must be, and what their order must be.
Rik
Rik 2021년 12월 20일
Function posted in duplicate question:
function [spei_index]=calculate_spei(t,prec,pevap,varargin);
[nlon, nlat, n]=size(prec);
prec=squeeze(prec);
for i=1:nlon
for j=1:nlat
if isnan(prec(i,j,1))==0
spei_index(i,j,:)=spei(t,prec(i,j,:),pevap(i,j,:),varargin);
else
spei_index(i,j,:)=ones(1,n-12)*NaN;
end
end
end

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2021년 12월 20일
spei_index(i,j,:) = spei(t, prec(i,j,:), pevap(i,j,:), varargin{:} );
is my guess.
  댓글 수: 5
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021년 12월 21일
so it should be okay right if my data:
prec=98x56x420
pevap=98x56x420
and
t=1x420
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021년 12월 21일
but, this is my error
Error using rect2cube (line 46)
Error: Dimensions of the gridsize must match the number of columns in A2.
Error in spei (line 117)
s = rect2cube(s,gridsize);

댓글을 달려면 로그인하십시오.


Rik
Rik 2021년 12월 20일
varargin is typically used to capture optional input arguments:
help varargin
VARARGIN Variable length input argument list. Allows any number of arguments to a function. The variable VARARGIN is a cell array containing the optional arguments to the function. VARARGIN must be declared as the last input argument and collects all the inputs from that point onwards. In the declaration, VARARGIN must be lowercase (i.e., varargin). For example, the function, function myplot(x,varargin) plot(x,varargin{:}) collects all the inputs starting with the second input into the variable "varargin". MYPLOT uses the comma-separated list syntax varargin{:} to pass the optional parameters to plot. The call, myplot(sin(0:.1:1),'color',[.5 .7 .3],'linestyle',':') results in varargin being a 1-by-4 cell array containing the values 'color', [.5 .7 .3], 'linestyle', and ':'. See also VARARGOUT, NARGIN, NARGOUT, INPUTNAME, FUNCTION, LISTS, PAREN. Documentation for varargin doc varargin
Since you don't use the optional/unnamed inputs, there shouldn't be any issues.
The function you're using should have documentation explaining how to use it. Read it. If you don't understand it, please provide a reference to where you got this function (as I can't find it).
  댓글 수: 2
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021년 12월 21일
I got the function from here.
I don't really understand it.
Walter Roberson
Walter Roberson 2021년 12월 21일
In that case my suggestion was correct.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Detection에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by