image thumbnail

gettok: a pedestrian matlab constructs decoder

by

 

16 Feb 2002 (Updated )

a decoder/tokenizer for MATLAB constructs

stokdemo
stokdemo
% GETTOK / STOK
% a pedestrian MATLAB constructs tokenizer/decoder
 
% - create some data
          clear k s w;
          format long g;
          rand('state',10);
 
% - a structure
          s.aoo{2,2}={{{rand(2,2,2),'TEST'}.'},{},{[]}};
          s.boo=[struct;struct([])];
          s.coo=struct('s',{},'t',[]);
 
% - a cell with some complicated contents including <s>
          k.v{1,1,1}=rand(3,3,2,2);
          k.v{1,2,1}=struct('foo',inline('a'));
          k.v{1,3,1}=[struct('goo',inline('a+b'))
                      struct('goo',inline('a+c'))];
          k.v{1,3,2}=[struct('goo','GOO');struct('goo',s.coo)];
          k.v{1,4,1}={'AN APPLE','TWO BANANAS'};
          k.v{1,5,1}=sqrt(-1);
          k.v{1,6,1}=[uint8(magic(3));rand(3)];
          k.v{1,7,1}=sparse(complex(rand(3)));
          k.v{1,8,1}=['THIS    ';'AND THAT';'     ALL'];
          k.v{1,9,1}=magic(3);
          k.v{1,9,2}=[s,s;s,s];
          k.w(1,1,1:2)=[s;s];
          k.w(1,1,3)=s;
 
% - run STOK
%   ... select entry nr 9 / 4
          w=stok(k);
 
 
 
% - show result
%   ... the complete output
           w
w = 
    version: '25-Apr-2005 09:06:44'
       mrel: '7.0.4.365 (R14) Service Pack 2'
       mver: 7
    runtime: '25-Apr-2005 09:20:12'
          n: 91
      field: {91x1 cell}
       desc: {91x1 cell}
        val: {}
       flag: [91x1 double]
         tx: [1x1 struct]
        opt: [1x1 struct]
       sver: '25-Apr-2005 09:06:54'
 
%   ... some CONSTRUCTORS
          w.field(1:15)
w.field = 
    'k.v{1,1,1}'
    'k.v{1,2,1}.foo'
    'k.v{1,3,1}(1,1).goo'
    'k.v{1,3,1}(2,1).goo'
    'k.v{1,4,1}{1,1}'
    'k.v{1,4,1}{1,2}'
    'k.v{1,5,1}'
    'k.v{1,6,1}'
    'k.v{1,7,1}'
    'k.v{1,8,1}'
    'k.v{1,9,1}'
    'k.v{1,1,2}'
    'k.v{1,2,2}'
    'k.v{1,3,2}(1,1).goo'
    'k.v{1,3,2}(2,1).goo.s'
 
%  w.field:       CONSTRUCTORS
%                 show the ML syntax to retrieve
%                 the contents of the construct
%                 at the command prompt
 
%   ... some DESCRIPTORS
          w.desc(1:15)
w.desc = 
    '@N.1(4:3,3,2,2:double) [288]'
    '@O.2(2:1,1:inline) [818]'
    '@O.2(2:1,1:inline) [870]'
    '@O.2(2:1,1:inline) [870]'
    '@STR >AN APPLE< [16]'
    '@STR >TWO BANANAS< [22]'
    '@N.1(2:1,1:complex.double) [16]'
    '@N.2(2:6,3:uint8) [18]'
    '@N.3(2:3,3:sparse.complex.double) [196]'
    '@T.1(2:3,8) [48]'
    '@N.4(2:3,3:double) [72]'
    '@N.5(0:double) [0]'
    '@N.6(0:double) [0]'
    '@STR >GOO< [6]'
    '@E.1(0:?????) [0]'
 
%  w.desc :       CONTENTS DESCRIPTORS
%                 a brief summary of the
%                 constructor's contents
%                 STRING         >string< [b]
%                 OTHER          @type.nri(size:class) [b]
%                 -type :        @C      cell
%                                @E      valid  empty constructor
%                                        cannot evaluate contents
%                                @N      numerical
%                                @O      object
%                                @S      struct
%                                @T      char array
%                 -nri  :        nr      of instance of type
%                 -size :        size    of contents
%                 -class:        class   of contents
%                 -b    :        bytes
 
% - evaluate CONSTRUCTOR nr 8
          cn=8;
          val=eval(w.field{cn})
          disp(sprintf('DESCRIPTOR:\n%s\n',w.desc{cn}));
          whos val
val =
 
    8    1    6
    3    5    7
    4    9    2
    0    1    1
    0    1    0
    1    0    1
 
DESCRIPTOR:
@N.2(2:6,3:uint8) [18]
 
  Name Size Bytes Class
  val  6x3  18    uint8 array
Grand total is 18 elements using 18 bytes
 

Contact us