comparison m-toolbox/classes/+utils/@helper/val2str.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % VAL2STR converts each value into a string
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: VAL2STR converts each value into a string.
5 %
6 % CALL: string = val2str(val)
7 % string = val2str(val, MAX_DISP);
8 %
9 % PARAMETERS: val: value which should be converted into a string.
10 % MAX_DISP: maximum string size. [default: 6000]
11 %
12 % REMARK: In a cell have each element the MAX_DISP size.
13 %
14 % VERSION: $Id: val2str.m,v 1.11 2010/12/17 14:21:59 hewitson Exp $
15 %
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
18 function varargout = val2str(varargin)
19
20 if nargin == 1
21 val = varargin{1};
22 MAX_DISP = 6000;
23 elseif nargin == 2
24 val = varargin{1};
25 MAX_DISP = varargin{2};
26 else
27 error('### Unknown number of inputs');
28 end
29
30
31 if ischar(val)
32 %%%%%%%%%% character %%%%%%%%%%
33
34 valStr = fitStr(sprintf('''%s''', val), MAX_DISP);
35
36 elseif isnumeric(val) || islogical(val)
37 %%%%%%%%%% numbers %%%%%%%%%%
38 if isvector(val)
39 if length(val)==1
40 valStr = sprintf('%.17g', val);
41 else
42 %%% Number is a vector
43 valStr = fitStr(utils.helper.mat2str(val), MAX_DISP);
44 if numel(valStr) > 3 && strcmp(valStr(end-2:end), '...')
45 valStr = sprintf('%s]', valStr);
46 end
47 end
48 elseif isempty(val)
49 valStr = '[]';
50 else
51 %%% Number is a matrix
52 valStr = fitStr(mat2str(val), MAX_DISP);
53 valStr = sprintf('[%dx%d], (%s)', size(val), valStr);
54 if numel(valStr) > 3 && strcmp(valStr(end-2:end), '...')
55 valStr = sprintf('%s]', valStr);
56 end
57 end
58
59 elseif iscell(val)
60 %%%%%%%%%% cell %%%%%%%%%%
61 if ~isempty(val)
62 valStr = '{';
63 for ii = 1:length(val)
64 valStr = sprintf('%s%s, ', valStr, utils.helper.val2str(val{ii}, MAX_DISP));
65 end
66
67 valStr = sprintf('%s}', valStr(1:end-2));
68 else
69 valStr = sprintf('{} [%dx%d]', size(val));
70 end
71 %%%%%%%%%%%%% symbols %%%%%%%%%%%%
72 elseif isa(val, 'sym')
73 valStr = fitStr(char(val), MAX_DISP);
74
75 elseif isa(val, 'history')
76 %%%%%%%%%% history objects %%%%%%%%%%
77 valStr = char(val); % sprintf('%dx[%s.hist]', numel(val), valStr{1});
78
79 elseif isa(val, 'provenance')
80 %%%%%%%%%% provenance objects %%%%%%%%%%
81 valStr = sprintf('%s %s', val.creator, val.ltpda_version);
82
83 elseif isa(val, 'database')
84 valStr = sprintf('database-object: %s', val.Instance);
85
86 elseif isobject(val)
87 %%%%%%%%%% all other objects objects %%%%%%%%%%
88 valStr = char(val);
89
90 elseif isjava(val)
91 %%%%%%%%%% java objects %%%%%%%%%%
92 valStr = class(val);
93
94 elseif isa(val, 'function_handle')
95 %%%%%%%%%% function handle %%%%%%%%%%
96 valStr = sprintf('function handle: %s', func2str(val));
97
98 else
99 valStr = sprintf('%dx%d [%s]', size(val,1), size(val,2), class(val));
100 end
101
102 varargout{1} = valStr;
103
104 end
105
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % Local Functions %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %
112 % FUNCTION: fitStr
113 %
114 % DESCRIPTION: Fits the string to the maximum displayed characters.
115 %
116 % HISTORY: 15-06-2009 Diepholz
117 % Creation.
118 %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120
121 function str = fitStr(str, MAX_DISP)
122 if length(str) > MAX_DISP
123 str = sprintf('%s ...', str(1:MAX_DISP));
124 end
125 end