Home > vbmeg > functions > common > utility > vb_struct2executable_str.m

vb_struct2executable_str

PURPOSE ^

Convert struct to string.

SYNOPSIS ^

function str = vb_struct2executable_str(s, struct_name)

DESCRIPTION ^

 Convert struct to string.
 The string can reproduce the structure using eval function.
 [USAGE]
    str = vb_struct2excutable_str(s [,struct_name]);
 [IN]
              s : <<struct>> structure.
    struct_name : (opt) <<string>> struct name. 
                    The program put the struct name into the string.
                    (i.e. 's.foo = ...')
                    'inputname' function is used for detecting the name of 
                    the struct. But in case of struct in array,
                    inputname cannot detect the struct name.
                    (error: vb_struct2_executable_str(structarray{1});)
                    Specify the second argument in such a case.
                    vb_struct2_executable_str(structarray{1}, 'structarray{1}');
 [OUT]
            str : converted string. eval(str) reproduce the original struct.

 [Restriction]
   - Over three-dimensional cell is not supported.
   - Over three-dimensional matrix is not supported.

 [example]
  s = struct;
  s.foo = 'test';
  s.bar = {'cell1', 'cell2'};
  s.val = [1,2,3; 4,5,6];

  str = vb_struct2executable_str(s)
  str =
  s.foo = 'test';
  s.bar{1,1} = 'cell1';
  s.bar{1,2} = 'cell2';
  s.val = [1  2  3; 4  5  6; ];

  eval(str); % reproduce s

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function str = vb_struct2executable_str(s, struct_name)
0002 % Convert struct to string.
0003 % The string can reproduce the structure using eval function.
0004 % [USAGE]
0005 %    str = vb_struct2excutable_str(s [,struct_name]);
0006 % [IN]
0007 %              s : <<struct>> structure.
0008 %    struct_name : (opt) <<string>> struct name.
0009 %                    The program put the struct name into the string.
0010 %                    (i.e. 's.foo = ...')
0011 %                    'inputname' function is used for detecting the name of
0012 %                    the struct. But in case of struct in array,
0013 %                    inputname cannot detect the struct name.
0014 %                    (error: vb_struct2_executable_str(structarray{1});)
0015 %                    Specify the second argument in such a case.
0016 %                    vb_struct2_executable_str(structarray{1}, 'structarray{1}');
0017 % [OUT]
0018 %            str : converted string. eval(str) reproduce the original struct.
0019 %
0020 % [Restriction]
0021 %   - Over three-dimensional cell is not supported.
0022 %   - Over three-dimensional matrix is not supported.
0023 %
0024 % [example]
0025 %  s = struct;
0026 %  s.foo = 'test';
0027 %  s.bar = {'cell1', 'cell2'};
0028 %  s.val = [1,2,3; 4,5,6];
0029 %
0030 %  str = vb_struct2executable_str(s)
0031 %  str =
0032 %  s.foo = 'test';
0033 %  s.bar{1,1} = 'cell1';
0034 %  s.bar{1,2} = 'cell2';
0035 %  s.val = [1  2  3; 4  5  6; ];
0036 %
0037 %  eval(str); % reproduce s
0038 %
0039 % Copyright (C) 2011, ATR All Rights Reserved.
0040 % License : New BSD License(see VBMEG_LICENSE.txt)
0041 
0042 %
0043 % --- Previous check
0044 %
0045 if ~exist('s', 'var')
0046     error('Structure is not specified.');
0047 end
0048 str = '';
0049 
0050 %
0051 % --- Main Procedure
0052 %
0053 if ~exist('struct_name', 'var')
0054     struct_name = inputname(1);
0055 end
0056 
0057 if iscell(s)
0058     elements = size(s);
0059     if length(elements) >= 3
0060         error('Over three dimension cell is not supported.');
0061     end
0062     for m=1:elements(1)
0063         for n=1:elements(2)
0064             element = s{m,n};
0065             if iscell(element) || isstruct(element)
0066                 str = [str, vb_struct2executable_str(element, [struct_name, '{', num2str(m), ',', num2str(n), '}'])];
0067             else
0068                 str = [str, Put(element, [struct_name, '{', num2str(m), ',', num2str(n), '}'])];
0069             end
0070         end
0071     end
0072 elseif isstruct(s)
0073 
0074     Nstruct = length(s);
0075     struct_name_m = struct_name;
0076     for m=1:Nstruct
0077         single_struct = s(m);
0078         fields = fieldnames(single_struct);
0079         Nfield = length(fields);
0080         if Nstruct>1
0081             struct_name_m = [struct_name, '(', num2str(m), ')'];
0082         end
0083         for k=1:Nfield
0084             field_name  = fields{k};
0085             field_value = single_struct.(field_name);
0086             if isstruct(field_value) || iscell(field_value)
0087                 str = [str, vb_struct2executable_str(field_value, [struct_name_m, '.', field_name])];
0088             else
0089                 str = [str, Put(field_value, [struct_name_m, '.', field_name])];
0090             end
0091         end
0092     end
0093 else
0094     str = [str, Put(s, struct_name)];
0095 end
0096 
0097 function str = Put(target, str)
0098 
0099 if ischar(target)
0100     % Put string(char)
0101     tmp = [' = ''' target ''';'];
0102     str = [str, tmp, sprintf('\n')];
0103 else
0104     % Put matrix
0105     tmp = ' = [';
0106     [M, N] = size(target);
0107     for m=1:M
0108         tmp = [tmp, num2str(target(m, :))];
0109         tmp = [tmp, '; '];
0110     end
0111     if ~isempty(target)
0112         tmp(end-1:end) = []; % remove tail '; '.
0113     end
0114     tmp = [tmp, '];'];
0115     if M==1 && N==1
0116         tmp = strrep(tmp, '[', '');
0117         tmp = strrep(tmp, ']', '');
0118     end
0119     str = [str, tmp, sprintf('\n')];
0120 end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005