-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddy_plot.m
More file actions
73 lines (59 loc) · 2.25 KB
/
addy_plot.m
File metadata and controls
73 lines (59 loc) · 2.25 KB
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
function varargout = addy_plot(varargin)
%ADDY_PLOT Plots data to any of the additional axes created with
%addyaxis(). Provide an axis struct created by addyaxis(), followed
%by any command that you would normally pass to plot().
% Use:
% plot_handle = addy_plot(ax_struct, <plot arguments>);
% Inspired by yyaxis,
% plotyyy (https://www.mathworks.com/matlabcentral/fileexchange/1017-plotyyy),
% and addaxis (https://www.mathworks.com/matlabcentral/fileexchange/9016-addaxis).
%% Argument parsing
narginchk(2, inf);
function_parser = inputParser;
function_parser.KeepUnmatched = true;
function_parser.PartialMatching = false;
% Required
requiredArguments = {'ax_struct'};
verificationFunction = @(potential_axes) ...
( isfield(potential_axes, 'axes_visible') && isfield(potential_axes, 'axes_hidden') );
for ii = 1:length(requiredArguments)
addRequired(function_parser, requiredArguments{ii}, verificationFunction)
end
% Optional
% optionalArguments = {};
% defaultOptional = {};
% for ii = 1:length(optionalArguments)
% addOptional(function_parser, optionalArguments{ii}, defaultOptional{ii})
% end
% % Parameters
% parameterArguments = {};
% defaultParameter = {};
% for ii = 1:length(parameterArguments)
% addOptional(function_parser, parameterArguments{ii}, defaultParameter{ii})
% end
% Parse
varargparsed = varargin(1:1);
parse(function_parser, varargparsed{:});
% Assign
% Take the first element of the struct, which is the one added last.
axes_hidden = function_parser.Results.ax_struct.axes_hidden(1);
axes_visible = function_parser.Results.ax_struct.axes_visible(1);
% Restore the active axes to the main axes after plotting.
main_axes = gca;
%% AddYPlot
% Plot visible data in hidden axes.
p = plot(axes_hidden, varargin{2:end});
% Copy automatically determined limits to the visible axes.
axes_visible.YLim = axes_hidden.YLim;
% Hide what needs to be hidden.
axes_hidden.Visible = 'off';
axes_hidden.HitTest = 'off';
axes_hidden.Toolbar.Visible = 'off';
axes_visible.Toolbar.Visible = 'off';
% Set the color of the y-axis to match the plot.
axes_visible.YColor = p.Color;
%% Outputs
varargout = {p};
% Restore the active axes to the main axes after plotting.
set(gcf, 'CurrentAxes', main_axes);
end