help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: nargin


From: insipido
Subject: Re: nargin
Date: Wed, 14 Jul 2010 15:10:12 -0300

Ok so it will tell me how many inputs I have in my function.
awesome... thank you very mucho

- vic -

On 14/07/2010, at 03:02 p.m., forkandwait wrote:

insipido <insipidbike <at> gmail.com> writes:


hello all,

You all have to excuse me for doing such a dumb question but... can
any one explain to me in the easiest way what NARGIN function does,

It tells you how many parameters have been passed to a function. Number of ARGuments In. There is a corresponding nargout, and some other related funcs.

Example:

function num_params = f()
  num_params = nargin();
endfunction

f(1,2,3,4)
ans =  4


_______________________________________________
Help-octave mailing list
address@hidden
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave


On 14/07/2010, at 03:03 p.m., address@hidden wrote:

This should go to Vic

example of using nargin function to 'qualify' the use of your custom
function:

function  [t,v]=fillinsteps(x,y,n);
%       FILLINSTEPS to smooth an ordinate varying waveform
%       use the form [t,v,n]=fillinsteps(x,y,n);
%               where x is the ordinate
%                        y is the abcissa
%                        n is the number of steps
if (nargin==0)
 help fillinsteps;
 return;
endif

if (nargin ~=3)
 help fillinsteps;
 return;
endif

[ry,cy]=size(y);
t=max(x)*(1:n)/n;
for i=1:n
 [xx,ix]=min(abs(x-t(i)));
 if (t(i)>=x(ix))
   if (ix==cy)
     v(i)=y(cy);
   else
     v(i)=y(ix)+(y(ix+1)-y(ix))*(t(i)-x(ix))/(x(ix+1)-x(ix));
   endif
 else
   if (ix==1)
     v(i)=y(1);
   else
     v(i)=y(ix-1)+(y(ix)-y(ix-1))*(t(i)-x(ix-1))/(x(ix)-x(ix-1));
   endif
 endif
endfor

Plus, you can use nargin to set default values [watch out for wordwrap]:

function [depthmils,depthmm]=skindepth(freq, conductivity, perm, exponent);

%       SKINDEPTH calculates the ADJUSTED skindepth of material
% use form [depthmils, depthmm] = skindepth( freq, [conductivity, [perm,
[exponent]]] )
%               freq in Hz
%               Matrix values are ok and made to match the freq matrix
%               optional conductivity in MS/m
%                       default conductivity matches 2024 aluminum, or 19.23 
MS/m
%               optional perm is permeability in relative value
%                       default perm value is 1
%               optional exponent is the value to "adjust" the skindepth
%                       default exponent value is 1.33     EMPIRICALLY DERIVED
%       returns "adjusted" skindepth in mils  and "adjusted" skindepth in mm
%       Note:   for Aluminum 6061-T3 with 25.56 MS/m conductivity
%               for Aluminum 2024-T3 with 5.20 microhm-cm
%                       19.23 MS/m
%               for titanium 6A4L with 180 microhm-cm
%                       0.5555 MS/m

if (nargin==0)
 help skindepth;
 return;
endif

if (nargin==1)
 conductivity = 19.23;
 perm=1;
 exponent=1.33;
endif

if (nargin==2)
 perm=1;
 exponent=1.33;
endif

if (nargin==3)
 exponent=1.33;
endif

if (nargin>4)
 disp("ERROR - too many arguments");
 return;
endif

%if ( (isscalar(exponent) != 1) + (iscomplex(exponent)==1) )
%  disp("ERROR - exponent MUST be a noncomplex scalar");
%  return;
%endif

[rowsf,columnsf]=size(freq);
[rowsc,columnsc]=size(conductivity);
[rowsp,columnsp]=size(perm);
[rowse,columnse]=size(exponent);

%       match all to frequency input
if (rowsc < rowsf)
 if (columnsc < columnsf)
   conductivity=ones(rowsf,columnsf)*conductivity(1,1);
 else
   conductivity=ones(rowsf,1)*conductivity(1,1:columnsf);
 endif
else
 if (columnsc < columnsf)
   conductivity=conductivity(1:rowsf,1)*ones(1,columnsf);
 else
   conductivity=conductivity(1:rowsf,1:columnsf);
 endif
endif

if (rowsp < rowsf)
 if (columnsp < columnsf)
   perm=ones(rowsf,columnsf)*perm(1,1);
 else
   perm=ones(rowsf,1)*perm(1,1:columnsf);
 endif
else
 if (columnsp < columnsf)
   perm=perm(1:rowsf,1)*ones(1,columnsf);
 else
   perm=perm(1:rowsf,1:columnsf);
 endif
endif

if (rowse < rowsf)
 if (columnse < columnsf)
   exponent=ones(rowsf,columnsf)*exponent(1,1);
 else
   exponent=ones(rowsf,1)*exponent(1,1:columnsf);
 endif
else
 if (columnse < columnsf)
   exponent=exponent(1:rowsf,1)*ones(1,columnsf);
 else
   exponent=exponent(1:rowsf,1:columnsf);
 endif
endif

%       make certain conductivity has sane values
if ( min(min(conductivity)) < 1e-12 )
 disp("ERROR - conductivity must be more than zero");
 return;
endif

%       make certain the conductivity is less than copper
if ( max(max(conductivity)) > 58)
 disp("ERROR - conductivity cannot be higher than copper");
 return
endif

%       make certain permeability is not less than 1
if ( min(min(perm)) < 1 )
 disp("ERROR - relative permeability cannot be less than one");
 return;
endif

%       make certain that the exponent is at least 1, or greater
if ( min(min(exponent)) < 1)
 disp("ERROR - exponent values must be equal to or larger than 1");
 return;
endif

%       find "adjusted" skindepth in millimeters
depthmm=1000*sqrt(2./ (2*pi()*freq*4*pi()*1e-7.*perm.*conductivity*1e6));
%       convert to mils
depthmils=(depthmm/.0254).^exponent;
depthmm=depthmils*.0254;

Regards,
Robert


looks like a blank response?
"insipido" <address@hidden> Wed, July 14, 2010 10:36 am
hello all,

You all have to excuse me for doing such a dumb question but... can
any one explain to me in the easiest way what NARGIN function does,
I just don't understand what it says in 'help nargin, and when
should I be using it ( I work with vectors and matrixes).

thank you for your help.
- vic -


_______________________________________________
Help-octave mailing list
address@hidden
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave



reply via email to

[Prev in Thread] Current Thread [Next in Thread]