function printTree(obj,objn,fid) % printTree - Print all definition for a structured object in a form % that later can be read back into matlab as a m-file. % % Parameter: obj The base object/structure to analyze % Parameter: objn The base object/structure name % Parameter: fid File id (from fopen) % % Example usage: printTree(ex,'ex') % % fileid=fopen('RawEx.m','w'); % fprintf(fileid,'global ex;\n'); % printTree(ex,'ex',fileid); % %% =========================================================================== %% =========================================================================== % Copyright (C): % 2005-2009 by Forschungszentrum Telekommunikation Wien, Austria; % All rights reserved. % Project : FTW's xDSLsimu % Author(s) : Tomas Nordstrom % % CVS: $Id: $ %% =========================================================================== % Change History % 2005-01-20 (ToNo) Created to debug/dump the ex structure % 2005-10-29 (ToNo) Set up so we can dump directly to a file % 2005-10-30 (ToNo) Fixed zero length strings %% =========================================================================== % Check input parameters if nargin<3, fid = 1; % 1 stdout (i.e. screen); 2 stderr end; if nargin<2, % ToDo: How do you get the name of an object, % i.e., if you have obj how do you get objn? objn='XobjectX'; end; if nargin<1, error('Not enough input arguments.'); end; sortfields = 1; % Flag to indicate that we want the fields sorted %% =========================================================================== ct=class(obj); % Get the class type of the object switch ct case 'struct' % If it is a structure just step in and recursively analyze the structure fnames=fieldnames(obj); if sortfields, fnames=sort(fnames); end; for fnix=1:size(fnames,1) printTree(obj.(fnames{fnix}), [objn '.' fnames{fnix}],fid); end case {'single','double'} % For numbers we use the handy mat2str to convert data into a string fprintf(fid,'%s = %s;\n',objn,mat2str(obj)); case 'char' norow = size(obj,1); if norow == 0 % Empty string fprintf(fid,'%s = '''';\n',objn); elseif norow == 1 % Strings are simple, except that we need to handle "'" in them fprintf(fid,'%s = ''%s'';\n',objn,strrep(obj,'''','''''')); else % Handle char arrays one row at the time for cix=1:norow fprintf(fid,'%s(%i,:) = ''%s'';\n',... objn,cix,strrep(obj(cix,:),'''','''''')); end; end case 'cell' % Cells are also done recursively, trying to take care about dimensions osize = size(obj); if osize(1)*osize(2) == 1 printTree(obj{1},sprintf('%s{1}',objn),fid); else % Assume first dim = 1, true in the simulator but maybe not always for xix = 1:osize(2) psize=size(obj{xix}); switch class(obj{1}) case 'cell' for yix = 1:psize(2) printTree(obj{xix}{yix},sprintf('%s{%d}{%d}',objn,xix,yix),fid); end case 'struct' for yix = 1:psize(2) printTree(obj{xix}(yix),sprintf('%s{%d}(%d)',objn,xix,yix),fid); end otherwise printTree(obj{xix},sprintf('%s{%d}',objn,xix),fid); end end end; otherwise % Otherwise of a type we do not handle at the moment osize = size(obj); fprintf(fid,'%s = [%s <%d,%d>];\n',objn,ct,osize(1),osize(2)); end