File: plot.m

package info (click to toggle)
octave-stk 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,020 kB
  • ctags: 384
  • sloc: ansic: 2,295; makefile: 25
file content (292 lines) | stat: -rw-r--r-- 7,536 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
% PLOT [overload base function]

% Copyright Notice
%
%    Copyright (C) 2013, 2014 SUPELEC
%
%    Author: Julien Bect  <julien.bect@supelec.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 plot (varargin)

% Parse the list of input arguments
[h, plot_elem, keyval_pairs] = parse_args_ (varargin{:});

% Read hold state
b = ishold ();

% Plot all
n = length (plot_elem);
for i = 1:n
    p = plot_elem(i);
    plot_ (h, p.x, p.z, p.S, keyval_pairs{:});
    if (i == 1) && (n > 1),  hold on;  end
end

% Restore hold state
if (~ b) && (n > 1),  hold off;  end

end % function plot


function plot_ (h, x, z, S, varargin)

%--- Handle stk_dataframe inputs ------------------------------------------

if isa (x, 'stk_dataframe'),
    xlab = x.colnames;
    x = double (x);
else
    xlab = {};
end

if isa (z, 'stk_dataframe'),
    zlab = z.colnames;
    z = double (z);
else
    zlab = {};
end

%--- Deal with various forms for x and z ----------------------------------

if isempty (x)  % Special: x not provided
    
    % Tolerate row vector for z
    if isrow (z),  z = z';  end
    
    % Create a default x
    x = (1:(size (z, 1)))';
    
else  % General case
    
    % Tolerate row vector for x
    if isrow (x),  x = x';  end
    
    % Number of points
    n = size (x, 1);
    
    % Tolerate row vector for z
    if isequal (size (z), [1 n]),  z = z';  end
    
end

%--- Plot and set labels --------------------------------------------------

if isempty (S)
    plot (h, x, z, varargin{:});
else
    plot (h, x, z, S, varargin{:});
end

if ~ isempty (xlab),
    if size (x, 2) == 1
        stk_xlabel (xlab{1});
    end
    % Note: in the case where x has several columns (and z also) we could
    % create more elaborate legends, e.g., "Zj versus Xj". Another time.
end

if ~ isempty (zlab)
    if size (z, 2) == 1,
        stk_ylabel (zlab{1});
    else
        legend (zlab{:});
    end
end

end % function plot_

%#ok<*SPWRN,*TRYNC>


function [h, plot_elem, keyval_pairs] = parse_args_ (arg1, varargin)

% Plot is highly polymorphic, making the task of parsing input arguments a
% rather lengthy and painful one...

%--- Formal grammar for the list of arguments -----------------------------
%
% Terminal symbols
%
%    h = a handle to an axes object
%    x = abscissa argument
%    z = ordinate argument
%    S = symbol/color/line string argument
%    k = key in a key-val pair
%    v = value in a key-val pair
%
% Derivation rules
%
%	<arg_list>          ::= <arg_list_0> | h <arg_list_0>
%   <arg_list_0>        ::= <plot_elem_list> <keyval_pairs>
%   <keyval_pairs>      ::= k v | <keyval_pairs> k v
%   <plot_elem_list>    ::= <plot_elem_single> | <plot_elem_several>
%	<plot_elem_several> ::= <plot_elem> | <plot_elem_several> <plot_elem>
%   <plot_elem_single>  ::= <plot_elem> | <special_plot_elem>
%	<plot_elem>         ::= x z | x z S
%   <special_plot_elem> ::= z | z S

% First, the easy bit: if the first argument can be interpreted as a handle,
% then it always is.

arg1_handle = false;
if isscalar (arg1) && isa (arg1, 'double'),
    try
        arg1_handle = strcmp (get (arg1, 'Type'), 'axes');
    end
end

if arg1_handle,
    h = arg1;
    if nargin == 1,
        stk_error ('Not enough input arguments.', 'NotEnoughInputArgs');
    else
        arg1 = varargin{1};
        varargin(1) = [];
    end
else
    h = gca;
end

% Then, arg1 *must* be a "numeric" argument
if ischar (arg1)
    stk_error ('Syntax error. Unexpected string argument.', 'TypeMismatch');
end

% Then, process remaining arguments recursively
if isempty (varargin)
    
    % Special case: x has been omitted, and there are no additional args
    plot_elem = struct ('x', [], 'z', {arg1}, 'S', []);
    keyval_pairs = {};
    
elseif ischar (varargin{1})
    
    % Special case: x has been omitted, and there *are* additional args
    if mod (length (varargin), 2)
        S = [];
        keyval_pairs = parse_keyval_ (varargin);
    else
        S = varargin{1};
        keyval_pairs = parse_keyval_ (varargin{2:end});
    end
    plot_elem = struct ('x', [], 'z', {arg1}, 'S', {S});
    
else
    
    % General case
    [plot_elem, keyval_pairs] = parse_args__ (arg1, varargin{:});
    
end

end % function parse_args_


function [plot_elem, keyval_pairs] = parse_args__ (x, z, varargin)

if ischar (x) || ischar (z)
    display (x);  display (z);
    stk_error (['Syntax error. At this point, we were expecting ' ...
        'another numeric (x, z) pair.'], 'SyntaxError');
end

if isempty (varargin)
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
    keyval_pairs = {};
    
elseif ~ ischar (varargin{1})  % expect another (x, z) pair after this one
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
    [plot_elem_, keyval_pairs] = parse_args__ (varargin{:});
    plot_elem = [plot_elem plot_elem_];
    
elseif length (varargin) == 1  % S
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', varargin(1));
    keyval_pairs = {};
    
elseif ischar (varargin{2})  % S, key, val, ...
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', varargin(1));
    keyval_pairs = parse_keyval_ (varargin{2:end});
    
elseif length (varargin) == 2  % key, val
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
    keyval_pairs = varargin;
    
elseif ~ ischar (varargin{3})  % S, x, z, ...
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', varargin(1));
    [plot_elem_, keyval_pairs] = parse_args__ (varargin{2:end});
    plot_elem = [plot_elem plot_elem_];
    
else  % key, val, key, val, ...
    
    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
    keyval_pairs = parse_keyval_ (varargin{:});
    
end

end % function parse_args__

function keyval_pairs = parse_keyval_ (key, val, varargin)

if nargin == 0,
    
    keyval_pairs = {};
    
elseif nargin == 1,
    
    errmsg = 'Syntax error. Incomplete key-value pair';
    stk_error (errmsg, 'NotEnoughInputArgs');
    
elseif ~ ischar (key)
    
    display (key);
    stk_error (['Syntax error. At his point, we were expecting a ' ...
        'key-value pair, but key is not a string.'], 'TypeMismatch');
    
else
    
    keyval_pairs = [{key, val} parse_keyval_(varargin{:})];
    
end

end % function parse_keyval_


%!test % plot with x as a vector and z as a (univariate) dataframe
%! x = linspace(0, 2*pi, 30)';
%! z = stk_dataframe(sin(x));
%! figure; plot(x, z); close(gcf);

%!test % plot with x as a vector and z as a (multivariate) dataframe
%! x = linspace(0, 2*pi, 30)';
%! z = stk_dataframe([sin(x) cos(x)], {'sin' 'cos'});
%! figure; plot(x, z); close(gcf);

%!test % plot with x as a dataframe and z as a vector
%! x = stk_dataframe(linspace(0, 2*pi, 30)');
%! z = sin(double(x));
%! figure; plot(x, z); close(gcf);