0

I want to create a relative axis in Matlab like the $\Delta I$-rulers in the following plot.

Before I start writing up a function that constructs it manually, I would like to know if there's way of creating an object with the NumericRuler-properties (like the default axes of a figure())

David E.E., Guttman N., and van Bergeijk W.A. (1959), “Binaural Interaction of High- Frequency Complex Stimuli,” J. Acoust. Soc. Am. 31(6), 774–782.

1 Answers1

0

So I ended up using the link provided by Sardar Usama's comment as inspiration and wrote a function to create an axes-object relative to the values of a "parent"-axes:

function ax = create_value_axes(hAx, pos)
%% ax = create_value_axes(hAx, pos)
%
%   Create axes at the value points of hAx.
%
%   pos(1) = x-position
%   pos(2) = y-position
%   pos(3) = x-width
%   pos(4) = y-width
%

% Get "parent" position and value limits
hAx_pos = hAx.Position;
hAx_xlm = hAx.XLim;
hAx_ylm = hAx.YLim;

% Get relative position increment pr value increment
x_step = hAx_pos(3) / (hAx_xlm(2) - hAx_xlm(1));
y_step = hAx_pos(4) / (hAx_ylm(2) - hAx_ylm(1));

% Set position
subaxes_abs_pos(1) = (pos(1)-hAx_xlm(1)) * x_step + hAx_pos(1);
subaxes_abs_pos(2) = (pos(2)-hAx_ylm(1)) * y_step + hAx_pos(2);
subaxes_abs_pos(3) = pos(3) * x_step;
subaxes_abs_pos(4) = pos(4) * y_step;

% Create axes
ax = axes('Position', subaxes_abs_pos);

% Remove background
ax.Color = 'none';

end

Sidenote: I found that I didn't need plotboxpos to get the correct positions of the "parent"-axes, using Matlab r2019b on macOS Mojave 10.14.6

Anyway, this is what I end up with: Resulting plot

Using the code:

% Just some random data
mockup_data_ild = [-10 -7 -4 0 4 7 10];

mockup_data_itd_45 = [-40 -20 -10 0 10 20 40];
mockup_data_itd_60 = [-30 -15 -5 0 5 15 30];

% Create figure
figure('Color', 'w')

x_axis_offset = [0 30];

hold on
% Plot 45 dB result
p1 = plot_markers(x_axis_offset(1) + mockup_data_ild, mockup_data_itd_45, ii);

% Plot 60 dB results
p2 = plot_markers(x_axis_offset(2) + mockup_data_ild, mockup_data_itd_60, ii);
p2.Color = p1.Color;
p2.HandleVisibility = 'off';

hold off

% Set axes properties
ax = gca;
ax.XAxis.TickValues = [x_axis_offset(1) x_axis_offset(2)];
ax.XAxis.TickLabels = {'45 dB' '60 dB'};
ax.XAxis.Limits = [x_axis_offset(1)-15 x_axis_offset(2)+15];
ax.XAxisLocation = 'top';

ax.YAxis.Limits = [-80 100];
ax.YAxis.Label.String = 'Interaural Time Difference, \Deltat, in samples';
ax.YGrid = 'on';

% Create 45 dB axis
ax2 = create_DeltaI_axis(ax, x_axis_offset(1));

% Create 60 dB axis
ax3 = create_DeltaI_axis(ax, x_axis_offset(2));

% Create legend
leg = legend(ax, {'P1'});
leg.Location = 'northwest';

%% Helpers
function ax = create_DeltaI_axis(hAx, x_pos)
    y_pos = -70;
    y_height = 170;
    range = 20;
    ax = create_value_axes(hAx, [x_pos-range/2 y_pos range y_height]);
    ax.XAxis.TickValues = [0 .25 .5 .75 1];
    ax.XAxis.TickLabels = {'-10'
                           '-5'
                           '0'
                           '5'
                           '10'};
    ax.XAxis.Label.String = '\DeltaI';
    ax.XGrid = 'on';
    ax.XMinorGrid = 'on';
    ax.YAxis.Visible = 'off';
end

function p = plot_markers(x, y, ii)
    markers = {'square','^', 'v', 'o', 'd'};
    p = plot(x, y);
    p.LineWidth = 1.5;
    p.LineStyle = 'none';
    p.Marker = markers{ii};
end
  • Please [edit](https://stackoverflow.com/posts/61008554/edit) your answer to include a sample of the missing variables/helper functions required to run your code for reproducibility – Sardar Usama Apr 03 '20 at 10:16