How to turn scatterplot labels on and off using uicontrol

2 views (last 30 days)
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

Answers (1)

Shubham Gupta
Shubham Gupta on 16 Oct 2019
Edited: Shubham Gupta on 22 Oct 2019
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!

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!