Erratic behavior with datacursormode while trying to delete plots on uiaxes

1 次查看(过去 30 天)
I'm writing some code where several channels are plotted into a uiaxes, then using datacursormode, channels can be clicked and deleted from the plot.
Problem:
  • The channel that gets deleted is erratic. Sometimes it deletes the channel as intended. Sometimes it doesn't do anything, other times it deletes other channels rather than the one selected.
  • If instead of this method, I have the "Delete Channel" close the figure, remove the selected channel from the plot list, then re-open/re-plot the data, everything works fine. However, this is very slow given the amount of data I'd like processed.
Example:
Here I have C20 selected and click Delete Channel:
Result: C20 remains, but C29 and C30 (the top two channels) were deleted
The problem can be replicated with the following code
function struct1 = operation_troubleshoot
% Draw UI
plotfig = uifigure('Units', 'normalized', 'Position',[.1 .1 .8 .6]);
plotgrd = uigridlayout(plotfig,[1 2]);
plotgrd.ColumnWidth = {'9x','1x'};
ax1 = uiaxes(plotgrd);
ax1.Layout.Column = 1;
plotDelete = uibutton(plotgrd,'push','Position', [.5 .5 .1 .1], 'Text', 'Delete Channel',...
'ButtonPushedFcn', @(btn,event) plotDeleteButtonPushed);
plotDelete.Layout.Row = 1;
plotDelete.Layout.Column = 2;
% generate fake data
data = [];
chans = {'C1'; 'C2'; 'C3'; 'C4'; 'C5'; 'C6'; 'C7'; 'C8'; 'C9'; 'C10';...
'C11'; 'C12'; 'C13'; 'C14'; 'C15'; 'C16'; 'C17'; 'C18'; 'C19'; 'C20';...
'C21'; 'C22'; 'C23'; 'C24'; 'C25'; 'C26'; 'C27'; 'C28'; 'C29'; 'C30'};
for i = 1:1:30
data.(chans{i}) = rand(1,500)*0.5;
end
%Create stacked plot:
t = linspace(0, 1, 500);
for i = 1:length(chans)
data.(chans{i}) = data.(chans{i})+1*i;
h(chans{i}) = plot(ax1, t, data.(chans{i}),'tag',sprintf(chans{i}));
hold(ax1, 'on')
end
y_limit = max(data.(chans{end}))+2;
ylim(ax1, [0 y_limit])
set(ax1,'YColor','none');
%Activate datacursormode
dcm = datacursormode(plotfig);
set(dcm,'Enable','on')
set(dcm,'UpdateFcn',@myupdatefcn)
%Datacursormode function
function txt = myupdatefcn(trash, event)
pos = get(event,'Position');
dts = get(event.Target,'Tag');
txt = {dts,...
['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))]};
struct1 = dts; %Passes Tag to struct1 to be used in the plotDeleteButtonPushed function
end
%Plot delete function
function plotDeleteButtonPushed
fprintf(['Removing ', struct1, '\n'])
delete(h(struct1))
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by