Home > functions > common > utility > vb_convert_var2text.m

vb_convert_var2text

PURPOSE ^

Save field name and its value for cell or structure array as text file

SYNOPSIS ^

function vb_convert_var2text(var, save_spec)

DESCRIPTION ^

 Save field name and its value for cell or structure array as text file
 [USAGE]
    vb_convert_var2text(<var>[,save_spec]);
 [IN]
        var : cell array or structure array
  save_spec : Option parameter
     .filename       : file name
     .contents       : = true  : Show field value [DEFAULT]
                       = false : Not show for field value
     .type_opt       : = true  : Show variable type
                     : = false : Not show for variable type [DEFAULT]
     .cell_opening   : = true  : Open cell array at the same level
                     : = false : Only show cell{1,1}  [DEFAULT]
     .struct_opening : = true  : Open structure array
                     : = false : Only show struct(1) [DEFAULT]

 セル・構造体配列変数の階層構造(フィールド名)と値をテキストファイルに保存
 [USAGE]
    vb_convert_var2text(<var>[,save_spec]);
 [IN]
        var : 変数
  save_spec : 保存オプション
     .filename
     .contents       : = true  : 変数の値を書き出す[DEFAULT]
                                 (ただしchar, 1xNで表される行列のみ)
                       = false : 変数の値を書き出さない
     .type_opt       : = true  : 型を書き出す
                     : = false : 型を書き出さない [DEFAULT]
     .cell_opening   : = true  : 同一レベルにあるセル変数を開く
                     : = false : 同一レベルにあるセル変数を開かない
                                 (cell{1,1}のみ表示 [DEFAULT]
     .struct_opening : = true  : 同一レベルにある構造体を開く
                     : = false : 同一レベルにある構造体を開かない
                                (struct(1)のみ表示) [DEFAULT]


 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 vb_convert_var2text(var, save_spec)
0002 % Save field name and its value for cell or structure array as text file
0003 % [USAGE]
0004 %    vb_convert_var2text(<var>[,save_spec]);
0005 % [IN]
0006 %        var : cell array or structure array
0007 %  save_spec : Option parameter
0008 %     .filename       : file name
0009 %     .contents       : = true  : Show field value [DEFAULT]
0010 %                       = false : Not show for field value
0011 %     .type_opt       : = true  : Show variable type
0012 %                     : = false : Not show for variable type [DEFAULT]
0013 %     .cell_opening   : = true  : Open cell array at the same level
0014 %                     : = false : Only show cell{1,1}  [DEFAULT]
0015 %     .struct_opening : = true  : Open structure array
0016 %                     : = false : Only show struct(1) [DEFAULT]
0017 %
0018 % セル・構造体配列変数の階層構造(フィールド名)と値をテキストファイルに保存
0019 % [USAGE]
0020 %    vb_convert_var2text(<var>[,save_spec]);
0021 % [IN]
0022 %        var : 変数
0023 %  save_spec : 保存オプション
0024 %     .filename
0025 %     .contents       : = true  : 変数の値を書き出す[DEFAULT]
0026 %                                 (ただしchar, 1xNで表される行列のみ)
0027 %                       = false : 変数の値を書き出さない
0028 %     .type_opt       : = true  : 型を書き出す
0029 %                     : = false : 型を書き出さない [DEFAULT]
0030 %     .cell_opening   : = true  : 同一レベルにあるセル変数を開く
0031 %                     : = false : 同一レベルにあるセル変数を開かない
0032 %                                 (cell{1,1}のみ表示 [DEFAULT]
0033 %     .struct_opening : = true  : 同一レベルにある構造体を開く
0034 %                     : = false : 同一レベルにある構造体を開かない
0035 %                                (struct(1)のみ表示) [DEFAULT]
0036 %
0037 %
0038 % Copyright (C) 2011, ATR All Rights Reserved.
0039 % License : New BSD License(see VBMEG_LICENSE.txt)
0040 
0041 %
0042 % --- Previous check
0043 %
0044 if ~exist('var', 'var')
0045     error('var is a required parameter.');
0046 end
0047 
0048 if ~exist('save_spec', 'var')
0049     save_spec = struct;
0050 end
0051 if ~isfield(save_spec, 'filename')
0052     save_spec.filename = 'var_struct.txt';
0053 end
0054 if ~isfield(save_spec, 'contents')
0055     save_spec.contents = true;
0056 end
0057 if ~isfield(save_spec, 'type_opt')
0058     save_spec.type_opt = false;
0059 end
0060 if ~isfield(save_spec, 'cell_opening')
0061     save_spec.cell_opening = false;
0062 end
0063 if ~isfield(save_spec, 'struct_opening')
0064     save_spec.struct_opening = false;
0065 end
0066 
0067 %
0068 % --- Main
0069 %
0070 
0071 % file open
0072 fid = fopen(save_spec.filename, 'wt');
0073 if fid == -1, return; end
0074 if save_spec.type_opt
0075     fprintf(fid, '[]は型名\n');
0076 end
0077 
0078 % root
0079 indent_level = 0;
0080 % 処理開始
0081 add_node(indent_level, fid, var, inputname(1), save_spec); 
0082 fclose(fid);
0083 
0084 % open file
0085 %edit(save_spec.filename);
0086 %
0087 % ----------------------------------------------------
0088 %
0089 function add_node(indent_level, fid, element, element_name, save_spec)
0090 % 構造体・セル配列の中を掘り進んで、セル・構造体以外の変数にあたれば表示する。
0091 
0092 % インデント設定
0093 indent = repmat('\t', 1, indent_level);
0094 element_info = whos('element');
0095 
0096 if isstruct(element)
0097     % 構造体表示処理
0098     % フィールド名を表示する
0099     Nstruct = length(element);
0100     if indent_level ~= 0 && save_spec.struct_opening == false && Nstruct>2
0101         Nstruct = 1;
0102     end
0103     for j=1:Nstruct
0104         if ~isempty(element_name)
0105             % セル配列内の構造体には名前がないので、表示しない
0106             fprintf(fid, [indent, '%s'], element_name);
0107             if Nstruct >= 2
0108                 fprintf(fid, '(%d)', j);
0109             end
0110             if save_spec.type_opt
0111                 fprintf(fid, ' : [%s]', element_info.class);
0112             end
0113             fprintf(fid, '\n');
0114         end
0115         names = fieldnames(element(j));
0116         for(k=1:length(names))
0117             add_node(indent_level+1, fid, element(j).(names{k}), ...
0118                     ['.' names{k}], save_spec);
0119         end
0120     end
0121 elseif iscell(element)
0122     % セル配列表示処理
0123     [m, n] = size(element);
0124     Nsize = length(element_info.size);
0125     if Nsize > 2 
0126         % 3次元以上のセルは表示しない
0127         dimStr = strrep(mat2str(element_info.size), ' ', 'x');
0128         fprintf(fid, [indent, '\tsize=%s, bytes=%d\n'], ...
0129                 dimStr, element_info.bytes);
0130         return;
0131     end
0132     if indent_level ~= 0 && save_spec.cell_opening == false, m=1; n=1; end
0133     for k=1:m
0134         for j=1:n
0135             % セル配列の中身をとってきて、型を調べる
0136             if save_spec.type_opt
0137                 cell_name = sprintf('%s{%d,%d} : [%s]', ...
0138                         element_name, k, j, element_info.class);
0139             else
0140                 cell_name = sprintf('%s{%d,%d}', element_name, k, j);
0141             end
0142             fprintf(fid, [indent,'%s\n'], cell_name);
0143             if strcmp(element_info.class, 'struct')
0144                 % セル内の構造体には名前がないので、
0145                 % 表示インデントレベルを上げない
0146                 add_node(indent_level, fid, element{k, j}, ...
0147                         '|__', save_spec);
0148             else
0149                 % セルの中身を表示する
0150                 add_node(indent_level+1, fid, element{k, j}, ...
0151                         '|__', save_spec);
0152             end
0153         end
0154     end
0155 else
0156     % 単独変数
0157     % 深さ優先探索で単独の変数になったらここにきて表示される
0158     if ~isempty(element_name)
0159         if save_spec.type_opt
0160             fprintf(fid, [indent,'%s : [%s]\n'], ...
0161                     element_name, element_info.class);
0162         else
0163             fprintf(fid, [indent,'%s\n'], element_name);
0164         end
0165         % 変数の中身保存OFFならここでリターン
0166         if save_spec.contents == false, return; end
0167         % 変数の中身保存処理
0168         Nsize = length(element_info.size);
0169         dimStr = strrep(mat2str(element_info.size), ' ', 'x');
0170         try
0171             if ischar(element) || (Nsize<=2 && element_info.size(1) == 1)
0172                 fprintf(fid, [indent, '\t%s\n'], mat2str(element));
0173             else
0174                 fprintf(fid, [indent, '\tsize=%s, bytes=%d\n'], ...
0175                         dimStr, element_info.bytes);
0176             end
0177         catch
0178             fprintf(fid, [indent,'\tCan''t display, variable type = %s\n'],...
0179              element_info.class);
0180         end
0181     end
0182 end

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005