如何解决如何从Matlab中的数据提示光标中提取任意数量的数据点?
我正在尝试显示一系列图形(在下面的示例代码中,2 个图形),以便我将逐个浏览每个图形并单击某些点并输出这些点的 (x,y) 位置从光标。根据一些在线帮助,我使用 pause
函数,单击一个点,按下一个键,使用 datacursormode
和 getCursorInfo
函数将 (x,y) 数据输出到 Matlab 终端然后点击下一个点。我不知道我要在每个图中选择多少个点,但假设它会小于 10。因此,我使用了一个带有暂停的 for
循环 (for rc1=1:10
)其中的声明。问题是当我完成较少数量的点(让我们说 rc1=5
)并移动到下一个数字时,我不知道如何退出这个循环。我该怎么做呢 ?如果代码可以表明我已完成从当前图形中选择点并让我跳出 rc1=1:10
循环(例如,if (condition) continue
之后的某种 if isfield ... end
语句应该工作)。
clc
clearvars
close all
for rc2=1:2
figure;
x = linspace(0,10,150);
y = cos(5*x);
fig = gcf;
plot(x,y)
% Enable data cursor mode
datacursormode on
dcm_obj = datacursormode(fig);
% Set update function
set(dcm_obj,'UpdateFcn',@myupdatefcn)
% Wait while the user to click
disp('Click line to display a data tip,then press "Return"');
for rc1=1:10
pause
% Export cursor to workspace
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct,'Position')
fprintf('%.2f %.2f \n',info_struct.Position);
end
end
end
function output_txt = myupdatefcn(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
pos = get(event_obj,'Position');
output_txt = {['x: ' num2str(pos(1))],['y: ' num2str(pos(2))]};
end
解决方法
好的,我想通了。在内部循环中添加一个 try,catch,break,end
序列并将 pause,getCursorInfo,isfield
段放入 try
部分使其工作。现在我选择几个点,只需关闭当前图形,按一个键并移动到下一个图形。这有效。
clc
clearvars
close all
for rc2=1:2
figure;
x = linspace(0,10,150);
y = cos(5*x);
fig = gcf;
plot(x,y)
% Enable data cursor mode
datacursormode on
dcm_obj = datacursormode(fig);
% Set update function
set(dcm_obj,'UpdateFcn',@myupdatefcn)
% Wait while the user to click
disp('Click line to display a data tip,then press "Return"');
for rc1=1:10
% Export cursor to workspace
try
pause
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct,'Position')
fprintf('%.2f %.2f \n',info_struct.Position);
end
catch
break;
end
end
end
function output_txt = myupdatefcn(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
pos = get(event_obj,'Position');
output_txt = {['x: ' num2str(pos(1))],['y: ' num2str(pos(2))]};
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。