How to turn scatterplot labels on and off using uicontrol

2 次查看(过去 30 天)
Hello dear matlabbers,
I tried to create a scatterplot with labels for each point (link here: 1).stack.jpg
Now I would like to give the user of the code the possibility to turn the labels on and off. I appreciate any help and comments! Thanks a lot in advance!
So far my code looks like this:
x = rand(1,100); y = rand(1,100); pointsize = 30;
idx = repmat([1 : 10], 1, 10) % I used class memberships here
figure(1)
MDSnorm = scatter(x, y, pointsize, idx, 'filled');
dx = 0.015; dy = 0.015; % displacement so the text does not overlay the data points
T = text(x + dx, y +dy, labels);
colormap( jet ); % in my code I use a specific colormap
Button = uicontrol('Parent',figure(2),'Style','toggle','String',...
'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
'callback',{@pb_call, MDSnorm, ???});
At the end of my script I then tried to define the pb_call function. I tried out several different versions, they all failed. Unfortunately, I am not skilled enough to translate any tutorials to my specific problem.
I have a rough idea what I need to do...
function [] = pb_call( ??? )
if get(Button, 'Value')
T --> invisible % ???
else
T --> visible % ???
end
end

回答(1 个)

Shubham Gupta
Shubham Gupta 2019-10-16
编辑:Shubham Gupta 2019-10-22
Try the following method (it might work for you but there are some limitations) :
x = rand(1,100); y = rand(1,100); pointsize = 30;
idx = repmat([1 : 10], 1, 10) % I used class memberships here
figure(1)
MDSnorm = scatter(x, y, pointsize, idx, 'filled');
dx = 0.015; dy = 0.015; % displacement so the text does not overlay the data points
T = text(x + dx, y +dy, labels);
colormap( jet ); % in my code I use a specific colormap
Button = uicontrol('Parent',figure(2),'Style','toggle','String',...
'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
'callback',@pb_call);
And the function can be edited as follow:
function pb_call(src,event)
a = gcf; % get the handle for current figure
T = findobj(a,'type','text');
if src.Value == 1
set(T,'Visible','off')
else
set(T,'Visible','on')
end
end
Limitations:
Only works for the current figure ( maybe best if only 1 figure is opened)
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by