Как использовать тумблер для включения / выключения надписей на графике рассеяния - PullRequest
2 голосов
/ 16 октября 2019

Я попытался создать точечный график с метками для каждой точки:

image Теперь я хотел бы дать пользователю кода возможность включать метки иoff.

Пока мой код выглядит так:

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, ???});

В конце моего сценария я попытался определить функцию pb_call. Я опробовал несколько разных версий, все они потерпели неудачу.

У меня есть приблизительное представление о том, что мне нужно делать. Что-то вроде:

function [] = pb_call( ??? )

if get(Button, 'Value')
    T --> invisible       % ???
else
    T --> visible         % ???
end
end

Как я могу изменить вышеперечисленное, чтобы включить или выключить ярлыки по желанию?

1 Ответ

0 голосов
/ 17 октября 2019

Вот рабочий пример:

x = rand(1,9); y = rand(1,9); pointsize = 30;
idx = repmat(1 : 3, 1, 3);             % I used class memberships here

labels = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j'};

h_fig = figure(1); %Create a figure, an keep the handle to the figure.
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

%Add a button to the same figure (the figure with the labels).
Button = uicontrol('Parent',h_fig,'Style','toggle','String',...
    'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
    'callback',@pb_call);


function pb_call(src, event)
    %Callback function (executed when button is pressed).
    h_fig = src.Parent; %Get handle to the figure (the figure is the parent of the button).
    h_axes = findobj(h_fig, 'Type', 'Axes'); %Handle to the axes (the axes is a children of the figure).
    h_text = findobj(h_axes, 'Type', 'Text'); %Handle to all Text labels (the axes is the parent of the text labels).

    %Decide to turn on or off, according to the visibility of the first text label.
    if isequal(h_text(1).Visible, 'on')
        set(h_text, 'Visible', 'off'); %Set all labels visibility to off
    else
        set(h_text, 'Visible', 'on');  %Set all labels visibility to on
    end
end

Пояснения в комментариях.

...