Matlab 动态输入变量和嵌套函数、匿名函数

一、简介

当输入的参数有不确定的时候就需要dynamic  input  variable .

 nargin==2 % nargin 是输入变量的数量,

myfunc_areaCircum_d2(rad,varargin)  nargin是说( 里面所有的参数)

function  [area,circum]=myfunc_areaCircum_d(varargin)
 rad=varargin{1};

 if nargin==2 % nargin 是变量的数量
     unit=varargin{2};
     if unit=='i'
         rad=rad*12;
     end
 end
 
 area=pi*rad.^2;
 circum=2*pi*rad;
end

 修改:

function  [area,circum]=myfunc_areaCircum_d2(rad,varargin)
 if nargin==2 % nargin 是变量的数量
     unit=varargin{1};
     if unit=='i'
         rad=rad*12;
     end
 end
 
 area=pi*rad.^2;
 circum=2*pi*rad;
end

>>  myfunc_areaCircum_d2(5,'i',9)  此时的nargin=3
2    if nargin==2 % nargin 是变量的数量
K>> nargin

ans =

     3

K>> 

练习:

function  sersum=mufunc_geomser(r,varargin)

    if nargin==1  % 如果传入的参数是一个
        n=randi([5,30]);
    elseif nargin==2
        n=varargin{1};
    else
        error('excetpting 1 or 2 input varibale(s).');
    end
    sersum=1+sum(r.^(1:n));
 
end

function [type,varargout]=myfunc_typesize(M)
   [r,c]=size(M);
    if r==1 && c==1
        type='s';
    elseif r==1||c==1
        type='v';
    else
        type='m'
    end


       if nargout ==1
           return ;
       elseif nargout ==2
           varargout{1}=max(r,c);

       elseif nargout ==3
            varargout{1}=r;
            varargout{2}=c;
       else
           error("wrong!");
        end

       
end

 test:

>> s=1

s =

     1

K>> v=1:3

v =

     1     2     3

K>> m=rand(3,4)

m =

    0.0975    0.9575    0.9706    0.8003
    0.2785    0.9649    0.9572    0.1419
    0.5469    0.1576    0.4854    0.4218

K>> t=myfunc_typesize(s)

t =

    's'

K>> t=myfunc_typesize(v)

t =

    'v'

K>> t=myfunc_typesize(m)

type =

    'm'


t =

    'm'

K>> [t,l]=myfunc_typesize(s)

t =

    's'


l =

     1

K>> [t,l]=myfunc_typesize(v)

t =

    'v'


l =

     3

K>> [t,l]=myfunc_typesize(m)

type =

    'm'


t =

    'm'


l =

     4

K>> 

二、嵌套函数

function ouvol=myfunc_example(len,wid,ht)
ouvol=base(len,wid)*ht;
  
    function outbase=base(len,wid)
        outbase=len*wid;
    end
end

改进之后的函数:


function ouvol=myfunc_example(len,wid,ht)
ouvol=base(len,wid)*ht;
  
    function outbase=base()
        outbase=len*wid;
    end
end

test

K>> myfunc_example(1,2,4)

ans =

     8
 

改进:

function ouvol=myfunc_example(len,wid,ht)
        len=len+1;
        wid=wid+1;
ouvol=base()*ht;
  
    function outbase=base()
        outbase=len*wid;
        len=len+3;
        wid=wid+4;
        inner_x=1;
      
        disp(inner_x);
    end
 
end



test  :

K>> myfunc_example(1,2,3)
     1


ans =

    18

K>> 

在大的function中定义的变量,可以在嵌套的函数中使用

function  outvol=myfunc_nested(len,wid,ht)
 x=2;
 outvol=base()*ht;
 function  baseout=base()
  baseout=len*wid*x;
end
 
end



test:
K>> myfunc_nested(1,2,3)

ans =

    12

K>> 

三、匿名函数

K>> cirarea=@(raduous)pi*raduous.^2;%匿名函数的使用
K>> cirarea(1)% 匿名函数的使用

ans =

    3.1416

K>> 


K>> class(cirarea)

ans =

    'function_handle'

K>> 


=============================================================================

K>> circlearea=@(len,wid,ht) len*wid*ht

circlearea =

  包含以下值的 function_handle:

    @(len,wid,ht)len*wid*ht

K>> circlearea(1,2,3)

ans =

     6

K>> 

-=============================================================================
K>> printfsss=@() fprintf('ssssss')

printfsss =

  包含以下值的 function_handle:

    @()fprintf('ssssss')

K>> printfsss()
ssssssK>> 

练习:

function myfunc_fn(fn)
 x=1:0.25:6;
 y=fn(x);
 plot(x,y,'ko');
end

test:
K>> fn=@sin

fn =

  包含以下值的 function_handle:

    @sin

K>> myfunc_fn(fn)
K>> 

 

 练习2:

function  myfunc_dot3(x,fn)
  y=fn(x);
  plot(x,y);
  xlabel('x');
  ylabel('y');
  title(['y=@s(x)' func2str(fn)]);
end



test:


K>> myfunc_dot3(0:0.01:1,@sin)
K>> 

 fplot:

K>> fplot(@sin,[0,2*pi])

 

 递归函数:

strtok 函数的使用

function  myfunc_printwords(sent)
  [word,rest]=strtok(sent);
  disp(word);
  if ~isempty(rest)
      myfunc_printwords(rest);
  end
end

>> myfunc_printwords('wge  agaf do i mmm')
2     [word,rest]=strtok(sent);
wge
agaf
do
i
mmm
>> 

 

 练习:

function f=myfunc_fab(n)
     if n<=2
       f=1;
     else
         f=myfunc_fab(n-2)+myfunc_fab(n-1);
     end
end

 

>> s=myfunc_fab(5)

s =

     5

>> s=myfunc_fab(6)

s =

     8

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值