0001 function vb_convert_var2text(var, save_spec)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
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
0069
0070
0071
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
0079 indent_level = 0;
0080
0081 add_node(indent_level, fid, var, inputname(1), save_spec);
0082 fclose(fid);
0083
0084
0085
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
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
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