comparison m-toolbox/classes/+utils/@xml/mat2str.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 % MAT2STR overloads the mat2str operator to set the precision at a central place.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: MAT2STR overloads the mat2str operator to set the
5 % precision at a central place.
6 %
7 % CALL: str = mat2str(number);
8 % str = mat2str(matrix);
9 %
10 % VERSION: $Id: mat2str.m,v 1.2 2010/04/27 15:41:31 ingo Exp $
11 %
12 % HISTORY: 26-07-2007 Diepholz
13 % Creation
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17 function str = mat2str(number)
18
19 MAX_PRECISION = 20;
20
21 s = size(number);
22 if s(1) ~= 1 && s(2) ~= 1
23 str = mat2str(number, MAX_PRECISION);
24
25 elseif isreal(number)
26
27 % For vectors it is faster to use sprintf directly
28 if s(1) ~= s(2)
29 str = '[';
30 else
31 str = '';
32 end
33 if s(1) > s(2)
34 str = [str sprintf('%.17g;', number)];
35 else
36 str = [str sprintf('%.17g ', number)];
37 end
38 if s(1) ~= s(2)
39 str = [str(1:end-1) ']'];
40 else
41 str = str(1:end-1);
42 end
43
44 else
45 str = mat2str(number, MAX_PRECISION);
46 end
47
48 % Make sure that the string have brackets (necessary for eval(str))
49 if ~isempty(str) && str(1) ~= '['
50 str = ['[' str ']'];
51 end
52
53 end
54