首页 > 解决方案 > 一次刷多个地块

问题描述

我有一系列我想以交互方式标记的行(使用brush)。为此,我尝试修改此处找到的类似代码。但是,该代码不适合我的用例,因为它是用单行编写的,而我有多行(绘制在单个轴上)。

因此,我扩展了代码以实现我的目标,如下所示:

function testbrushcode()
% Create data
t = 0:0.2:25;
x = sin(t);
y = tan(2.*t);
z = sin(2*t);

% Create figure with points

for i = 1:3

    myfig = figure();
    m{1} = plot(t, x);
    hold on
    m{2} = plot(t, y);
    m{3} = plot(t, z);
    hold off
    brush on;


    index_vec1{i} = brushed_Data_ids(myfig, m{i});


end


plot(t(index_vec1{1}), x(index_vec1{1}))
hold on
plot(t(index_vec1{2}), x(index_vec1{2}))
plot(t(index_vec1{3}), x(index_vec1{3}))
hold off

end

function [index_vec1] = brushed_Data_ids(myfig, m)

uicontrol('Parent', myfig, ...
    'Style', 'pushbutton',...
    'String', 'Get selected points index',...
    'Position', [5, 5, 200, 30],...
    'Units', 'pixels',...
    'Callback', {@mycallback, m} ...
    );

% ---> Now the user should select the points and click the button 'Get
% selected points index'
waitfor(myfig)



% Display index of selected points once the figure is closed
% disp(selectedPoints);
index_vec1 = [];
for i = 1:length(selectedPoints)
    if selectedPoints(i) == 1
        index_vec1 = [index_vec1 i];
    end
end

end

function mycallback(~, ~, mylineseries)
% Ignore the first 2 function inputs: handle of invoking object & event
% data

assignin('caller', 'selectedPoints', get(mylineseries,'BrushData'))

end

由于使用循环,代码中存在问题for- 因此,我必须将相同的数据刷三次。我想刷一次数据并获取所有三行的刷过索引(来自未刷过的数据)。

标签: matlablinematlab-figureinteractivematlab-gui

解决方案


试试这个稍微简化的代码:

function q60017140()
% Create data
t = 0:0.2:25;
x = sin(t);
y = tan(2*t);
z = sin(2*t);

% Create figure with points
hFig = figure('Position', [295,303,1014,626]); 
hAx = subplot(1,2,1,'Parent', hFig);
hLines = plot(hAx, t,x, t,y, t,z);

hAx(2) = subplot(1,2,2);

uicontrol('Parent', hFig, ...
  'Style', 'pushbutton',...
  'String', 'Get selected points index',...
  'Position', [5, 5, 200, 30],...
  'Units', 'pixels',...
  'Callback', {@brushCallback, hLines, hAx} ...
  );

brush(hFig, 'on');

end

function brushCallback(~, ~, hLines, hAx)
index_vec = cellfun(@logical, get(hLines,'BrushData'), 'UniformOutput', false);

plot(hAx(2), ...
  hLines(1).XData(index_vec{1}), hLines(1).YData(index_vec{1}), ...
  hLines(2).XData(index_vec{2}), hLines(2).YData(index_vec{2}), ...
  hLines(3).XData(index_vec{3}), hLines(3).YData(index_vec{3}));
end

在此处输入图像描述


推荐阅读