Home > atmlab > handy > struct2string_compact.m

struct2string_compact

PURPOSE ^

struct2string_compact

SYNOPSIS ^

function descr = struct2string_compact(S, varargin)

DESCRIPTION ^

 struct2string_compact

 Return a compact representation of a struct. Whole numbers are represented
 as such, any others are given N digits of precision. This can be useful
 to add to a filename for storing a figure or to use in a plot title or legend.

 EXAMPLE

 >> disp(struct2string_compact(struct('A', 1, 'B', 2, 'C', pi), 4));
 A=1,B=2,C=3.1416

 FORMAT

   descr = struct2string_compact(S, n)

 IN

   S   scalar structure, all values should be scalar numbers
   n   number of digits of precision. Defaults to 3.

 OUT

   descr   compact string describing the structure

 $Id: struct2string_compact.m 7445 2012-03-26 10:15:27Z seliasson $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

struct2string_compact.m

SOURCE CODE ^

0001 function descr = struct2string_compact(S, varargin)
0002 
0003 % struct2string_compact
0004 %
0005 % Return a compact representation of a struct. Whole numbers are represented
0006 % as such, any others are given N digits of precision. This can be useful
0007 % to add to a filename for storing a figure or to use in a plot title or legend.
0008 %
0009 % EXAMPLE
0010 %
0011 % >> disp(struct2string_compact(struct('A', 1, 'B', 2, 'C', pi), 4));
0012 % A=1,B=2,C=3.1416
0013 %
0014 % FORMAT
0015 %
0016 %   descr = struct2string_compact(S, n)
0017 %
0018 % IN
0019 %
0020 %   S   scalar structure, all values should be scalar numbers
0021 %   n   number of digits of precision. Defaults to 3.
0022 %
0023 % OUT
0024 %
0025 %   descr   compact string describing the structure
0026 %
0027 % $Id: struct2string_compact.m 7445 2012-03-26 10:15:27Z seliasson $
0028 
0029 n = optargs(varargin, {3});
0030 
0031 if isempty(fieldnames(S))
0032     descr = '';
0033     return;
0034 end
0035 %descr = cellfun(@(v) sprintf('%s=%.*f,', v, n*~iswhole(S.(v)), S.(v)), fieldnames(S), 'UniformOutput', false);
0036 descr = cellfun(@(v) sprintf('%s=%s;', v, elem2str(S.(v), n)), fieldnames(S), 'UniformOutput', false);
0037 descr = strcat(descr{:});
0038 descr = descr(1:end-1); % take off final comma
0039 end
0040 function s = elem2str(v, n)
0041 % elem2str Convert single element to string compactly
0042 if isempty(v)
0043     s = '';
0044     return
0045 elseif isboolean(v) || istensor0(v) % isscalar is true for cell array with 1 elem
0046     s = sprintf('%.*f', n*~(iswhole(v)||isboolean(v)), v);
0047 elseif iscell(v)
0048     s = cellfun(@(u) [elem2str(u, n) ','], v, 'UniformOutput', false);
0049 elseif isscalar(v)
0050     s = char(v);
0051 elseif isvector(v)
0052     s = arrayfun(@(u) [elem2str(u, n) '-'], v, 'UniformOutput', false);
0053 else
0054     error('atmlab:struct2string_compact:elem2str', ...
0055         'Unable to provide compact string representation of %s', ...
0056         evalc('disp(v)'));
0057 end
0058 if ~isscalar(v)
0059     s = strcat(s{:});
0060     s = s(1:end-1);
0061 end
0062 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005