Skip to content
Snippets Groups Projects
Verified Commit dd4404c4 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Add two arguments to dplot routine.

--style followed by the name of a matlab script (without extension). This matlab
script can be used to apply styling commands to the produced plot.

--title followed by a row char array, used to set the plot title.
parent 09846315
Branches
No related tags found
No related merge requests found
Pipeline #8737 passed
......@@ -7,7 +7,7 @@ function dplot(varargin)
% >> toto = dseries(randn(100,3), dates('2000Q1'), {'x','y','z'});
% >> noddy = dseries(randn(100,3), dates('2000Q1'), {'x','y','z'});
% >> b = 3;
% >> dplot --expression 2/b*cumsum(x/y(-1)-1) --dseries toto --dseries noddy --range 2001Q1:2024Q1
% >> dplot --expression 2/b*cumsum(x/y(-1)-1) --dseries toto --dseries noddy --range 2001Q1:2024Q1 --title 'This is my plot'
%
% Will produce plots of 2*cumsum(x/y(-1)-1), where x and y are variables in objects toto and noddy,
% in the same figure.
......@@ -15,7 +15,9 @@ function dplot(varargin)
% INPUTS
% --expression followed by a mathematical expression involving variables available in the dseries objects, dseries methods, numbers or parameters.
% --dseries followed by the name of a dseries object available in the workspace.
% --range followed by a dates range
% --range followed by a dates range.
% --style followed by the name (without extension) of a matlab script, can be used to apply styling commands to the produced plot.
% --title followed by a row char array, sets the plot title.
% --with-legend prints a legend below the produced plot.
%
% REMARKS
......@@ -143,6 +145,19 @@ function dplot(varargin)
if iswithlegend(varargin)
legend(legendnames{:}, 'Location','SouthOutside','Orientation','horizontal','Box','off')
end
% Styling
script = getstyle(varargin);
if ~isempty(script)
eval(script)
end
% Set title
str = gettile(varargin);
if ~isempty(str)
str
title(str)
end
end
function expr = getexpressions(cellarray)
......@@ -220,7 +235,50 @@ function range = getrange(cellarray)
end
function [epos, dpos, rpos, zpos] = positions(cellarray)
function script = getstyle(cellarray)
% Return period range for the plots.
%
% INPUTS
% - cellarray [char] 1×n cell array of row char arrays.
%
% OUTPUTS
% - script [char] name of the styling script
[~, ~, ~, ~, spos] = positions(cellarray);
if isempty(spos)
script = '';
return
end
script = cellarray{spos+1};
end
function title = gettile(cellarray)
% Return period range for the plots.
%
% INPUTS
% - cellarray [char] 1×n cell array of row char arrays.
%
% OUTPUTS
% - script [char] name of the styling script
[~, ~, ~, ~, ~, tpos] = positions(cellarray);
if isempty(tpos)
title = '';
return
end
title = cellarray{tpos+1};
end
function [epos, dpos, rpos, zpos, spos, tpos] = positions(cellarray)
% Return positions of the arguments.
%
......@@ -233,7 +291,7 @@ function [epos, dpos, rpos, zpos] = positions(cellarray)
% - rpos [integer] scalar, index of the --range argument.
% - zpos [integer] first index of non --expression argument.
% Indices for --expression arguments.
% Indices for --expression arguments.
epos = find(strcmp('--expression', cellarray));
if isempty(epos)
error('dplot::positions: --expression argument is mandatory.')
......@@ -245,7 +303,7 @@ function [epos, dpos, rpos, zpos] = positions(cellarray)
end
% Index for --range argument.
rpos = find(strcmp('--range', cellarray));
assert(length(rpos)==1 || length(rpos)==0, 'dplot::positions: Only one range is allowed.')
assert(length(rpos)==1 || isempty(rpos), 'dplot::positions: Only one range is allowed.')
% Define index for the first argument name different from --expression
if isempty(rpos)
zpos = dpos(1);
......@@ -253,7 +311,13 @@ function [epos, dpos, rpos, zpos] = positions(cellarray)
zpos = min(dpos(1), rpos);
end
% Check that expressions are coming before other arguments
assert(max(epos)<zpos, 'dplot::positions: --expression must come befor the other arguments.')
assert(max(epos)<zpos, 'dplot::positions: --expression must come before the other arguments.')
% Index for --style argument
spos = find(strcmp('--style', cellarray));
assert(length(spos)==1 || isempty(spos), 'dplot::positions: Only one style script is allowed.')
% Index for --title argument
tpos = find(strcmp('--title', cellarray));
assert(length(tpos)==1 || isempty(tpos), 'dplot::positions: Only one title is allowed.')
end
function m = allowedmethods()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment