matlab - 一次刷多个地块
问题描述
我有一系列我想以交互方式标记的行(使用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
- 因此,我必须将相同的数据刷三次。我想刷一次数据并获取所有三行的刷过索引(来自未刷过的数据)。
解决方案
试试这个稍微简化的代码:
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
推荐阅读
- angular - 声明时未定义的变量
- cobol - 如何在 COBOL 中恢复 EXEC CICS 的默认条件处理?
- javascript - 正则表达式 - 如何排除包含特定字符的特定字符串
- java - 如何为自定义布局适配器android studio创建搜索过滤器
- php - 如果 'ID'=='ID' 则循环检查复选框并显示检查
- r - \Sexpr 如果包含 LaTeX 命令,则无法解析和评估
- android - Firebase cloudfunctions对象不能用json android编码
- javascript - 我如何解决可能的未处理承诺拒绝?
- java - 在 log4j2 2.13 版本中扩展 RollingFileAppender 的几个方法
- javascript - 如何在一行中从单独的解构属性中解构?