Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slstructsize
Home > sltoolbox > utils > slstructsize.m

slstructsize

PURPOSE ^

SLSTRUCTSIZE Compute the size of a struct

SYNOPSIS ^

function nbytes = slstructsize(S, fields, types)

DESCRIPTION ^

SLSTRUCTSIZE Compute the size of a struct

 $ Syntax $
   - nbytes = slstructsize(S)
   - nbytes = slstructsize(S, fields)
   - nbytes = slstructsize(S, fields, types)

 $ Arguments $
   - nbytes:            the number of bytes in the specified parts of the struct
   - fields:            the fields that are counted in the computation
   - types:             the types of the fields when output

 $ Description $

   - nbytes = slstructsize(S) computes the bytes of the struct occupied
     if S is a struct-array, then the resultant nbytes is sum of all sizes
     of struct entries.

   - nbytes = slstructsize(S, fields) only computes the specified fields.
     If some specified fields are not in S, an error will be raised.

   - nbytes = slstructsize(S, fields, types) the computation will
     use the types for computing the size instead of using original types.

 $ History $
   - Created by Dahua Lin on Dec 10th, 2005

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slstructsize SLSTRUCTSIZE Compute the size of a struct
  • sltypesize SLTYPESIZE Gets the element size of a specified type
This function is called by:

SOURCE CODE ^

0001 function nbytes = slstructsize(S, fields, types)
0002 %SLSTRUCTSIZE Compute the size of a struct
0003 %
0004 % $ Syntax $
0005 %   - nbytes = slstructsize(S)
0006 %   - nbytes = slstructsize(S, fields)
0007 %   - nbytes = slstructsize(S, fields, types)
0008 %
0009 % $ Arguments $
0010 %   - nbytes:            the number of bytes in the specified parts of the struct
0011 %   - fields:            the fields that are counted in the computation
0012 %   - types:             the types of the fields when output
0013 %
0014 % $ Description $
0015 %
0016 %   - nbytes = slstructsize(S) computes the bytes of the struct occupied
0017 %     if S is a struct-array, then the resultant nbytes is sum of all sizes
0018 %     of struct entries.
0019 %
0020 %   - nbytes = slstructsize(S, fields) only computes the specified fields.
0021 %     If some specified fields are not in S, an error will be raised.
0022 %
0023 %   - nbytes = slstructsize(S, fields, types) the computation will
0024 %     use the types for computing the size instead of using original types.
0025 %
0026 % $ History $
0027 %   - Created by Dahua Lin on Dec 10th, 2005
0028 %
0029 
0030 %% parse and verify input arguments
0031 if ~isstruct(S)
0032     error('S is not a struct');
0033 end
0034 if nargin < 2 || isempty(fields)
0035     fields = fieldnames(S);
0036 end
0037 if nargin < 3 || isempty(types)
0038     use_origin_types = true;
0039 else
0040     use_origin_types = false;
0041 end
0042 
0043 %% compute
0044 n = numel(S);
0045 if n == 1
0046     
0047     nbytes = 0;
0048     nterms = length(fields);
0049     
0050     for t = 1 : nterms
0051         cur_term = S.(fields{t});
0052         if isempty(cur_term)
0053             continue;
0054         elseif isstruct(cur_term)
0055             cur_nbytes = slstructsize(cur_term);
0056         else
0057             if use_origin_types || isempty(types{t})
0058                 cur_elem_bytes = sltypesize(class(cur_term));
0059             else
0060                 cur_elem_bytes = sltypesize(types{t});
0061             end
0062             cur_nbytes = cur_elem_bytes * numel(cur_term);
0063         end
0064         
0065         nbytes = nbytes + cur_nbytes;
0066     end
0067     
0068 else
0069     
0070     nbytes = 0;
0071     for i = 1 : n
0072         if use_origin_types
0073             nbytes = nbytes + slstructsize(S(i), fields);
0074         else
0075             nbytes = nbytes + slstructsize(S(i), fields, types);
0076         end
0077     end
0078     
0079 end
0080 
0081 
0082 
0083 
0084 
0085 
0086

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com