Problem setting datatip positions in subplots within uipanel

5 views (last 30 days)
I am creating a gui that will allow the user to plot and analyze data. One of the features being developed is the ability to allow the user to manually place a data tip on one subplot. Then via a click of a button, data tips will be placed on the remaining subplots at that same position in time. This works fine as long as the subplots are children of the main figure. When a uipanel is the parent, the data tips get created, but at the wrong positions. It seems like something is getting lost in translation when creating/plotting/accessing the plot data. I've tried different combinations of pauses and drawnow's, but to no avail. Due to the other features/layout of this gui, it would be ideal to keep the subplots parented by the uipanel.
Below I made a test script mimicing the functionality. If figParent is true, everything works fine. Setting it to false, the data tip positions are wrong, even though the print out to the screen is correct. This seems to get worse as the size of the data grows or if the figure is docked. Any help that can be provided would be greatly appreciated.
close all;
% Set randstream for repeatability
stream = RandStream.create('mrg32k3a','NumStreams',1000,'StreamIndices',1);
n = 1000;
x = linspace(0,500,n);
y1 = x.^2;
y2 = sin(x);
y3 = randi(x(end),1,length(x));
% Creat figure
f = figure;
f.Position = get(0,'ScreenSize');
f.Units = 'normalized';
% Decide parent of subplots
figParent = 0;
if figParent
iParent = f;
p = uipanel;
p.Units = 'normalized';
p.Parent = f;
p.Position = f.Position;
iParent = p;
% Plot Data
h(1) = subplot(1,3,1);
h(1).Parent = iParent;
a(1) = plot(x,y1);
grid on;
h(2) = subplot(1,3,2);
h(2).Parent = iParent;
a(2) = plot(x,y2);
grid on;
h(3) = subplot(1,3,3);
h(3).Parent = iParent;
a(3) = plot(x,y3);
grid on;
% Initialize data cursor
dcm = datacursormode(f);
% Use dataIndex to set desired position.
dataIndex = 10;
% Create data tips for each subplot
datacursors = nan(size(h));
for i = 1:length(h)
lines = findobj(h(i),'Type','line');
datacursors(i) = dcm.createDatatip(lines);
% Set the position of each datatip based on the dataIndex
for i = 1:length(h)
lines = findobj(h(i),'Type','line');
pos = [lines.XData(dataIndex), lines.YData(dataIndex)];
fprintf('Position for subplot % i, X = %f, Y = %f\n',i,pos(1),pos(2));

Answers (1)

Robert Kraemer
Robert Kraemer on 8 Feb 2022
Even when I put figParent on true, it doesn't synchronizes...




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!