summaryrefslogtreecommitdiff
path: root/inst/misc/text/stk_disp_getformat.m
blob: 1de8a92b441fafd119eeb21bee469963d7029be4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
% STK_DISP_GETFORMAT [STK internal]
%
% CALL: [FMT, SPC] = stk_disp_getformat ()
%
% NOTE
%
%    This function solves a Matlab/Octave compatibility issue.  See:
%
%     * https://savannah.gnu.org/bugs/?51035
%     * https://savannah.gnu.org/bugs/?49951
%     * https://savannah.gnu.org/bugs/?46034
%     * https://sourceforge.net/p/kriging/tickets/73

% Copyright Notice
%
%    Copyright (C) 2017 CentraleSupelec
%
%    Authors:  Julien Bect  <julien.bect@centralesupelec.fr>

% Copying Permission Statement
%
%    This file is part of
%
%            STK: a Small (Matlab/Octave) Toolbox for Kriging
%               (http://sourceforge.net/projects/kriging)
%
%    STK is free software: you can redistribute it and/or modify it under
%    the terms of the GNU General Public License as published by the Free
%    Software Foundation,  either version 3  of the License, or  (at your
%    option) any later version.
%
%    STK is distributed  in the hope that it will  be useful, but WITHOUT
%    ANY WARRANTY;  without even the implied  warranty of MERCHANTABILITY
%    or FITNESS  FOR A  PARTICULAR PURPOSE.  See  the GNU  General Public
%    License for more details.
%
%    You should  have received a copy  of the GNU  General Public License
%    along with STK.  If not, see <http://www.gnu.org/licenses/>.

function [fmt, spc] = stk_disp_getformat ()

persistent method
if isempty (method)
    method = choose_method ();
end

switch method
    
    case 1  % Matlab and some versions of Octave (e.g., 4.0.3)
        
        % Note: 'Format' and 'FormatSpacing' have been removed from the public
        % list of root properties since Matlab R2014b, but are still recommended
        % as the proper way to obtain this information in R2017a's manual.
        
        fmt = get (0, 'Format');
        spc = get (0, 'FormatSpacing');
        
    case 2  % Octave 4.2 for sure (when was it introduced ?)
        
        % In Octave 4.2.0 the 'Format' and 'FormatSpacing' root property have
        % been removed, but __formattring__ and __compactformat__ are still
        % available.
        
        fmt = feval ('__formatstring__');
        spc = feval ('__compactformat__');
        
    case 3  % Octave > 4.2
        
        % After Octave 4.2.0, __formattring__ and __compactformat__ have been
        % removed too, and a new syntax is available.
        
        [fmt, spc] = format ();
        
    otherwise
        
        % If nothing works, I really don't know which version of Octave or
        % Matlab you are using, but defaulting to 'short' + 'loose' in this case
        % seems better than a warning or an error.
        
        fmt = 'short';
        spc = 'loose';
        
end % switch

end % function


function method = choose_method ()

try
    
    spc = get (0, 'FormatSpacing');
    assert (ismember (spc, {'compact', 'loose'}));
    method = 1;
    
catch
    
    if exist ('__compactformat__', 'builtin') == 5
        
        method = 2;
        
    else
        
        try
            
            [fmt, spc] = format (); %#ok<ASGLU> CG#07
            assert (ismember (spc, {'compact', 'loose'}));
            method = 3;
            
        catch
            
            method = 4;
            
        end % try
        
    end % if
    
end % try

end % function