您可以使用 while 循环来选择两个点,并使用嵌套函数来共享变量。
使用嵌套函数是将选定坐标和计数器传递给父函数的一种可能解决方案。
您可以使用 while 循环将选定(标记)点的数量限制为两个。
在 while 中放置 pause 对于响应能力很重要。
这是一个工作代码示例:
function ellip()
%Parent function (use a function instead of a script,
%allows sharing variables with internal function).
clc;
%clear;
a=3;
b=7;
x0=0;
y0=0;
t=-pi:0.01:pi;
x=x0+a*cos(t);
y=y0+b*sin(t);
sz = 5;
h = figure; %Open new figure, and keep the handle.
scatter(x,y,sz);
dcm_obj = datacursormode;
dcm_obj.Enable = 'on';
set(dcm_obj,'UpdateFcn',@myupdatefcn)
%pos_x, pos_y and pos_counter are known both in scope of myupdatefcn and in ellip
pos_x = zeros(1,2);
pos_y = zeros(1,2);
pos_counter = 0;
%Loop until two points are selected
while (pos_counter < 2) && isvalid(h)
pause(0.01);
end
if isvalid(h)
close(h); %Close the figure;
%Display position of the two selected coordinates.
fprintf('pos1 = (%f, %f), pos2 = (%f, %f)\n', pos_x(1), pos_y(1), pos_x(2), pos_y(2));
end
dcm_obj.delete() %Delete the object (cleanup)
%myupdatefcn is a nested function, inside ellip function
function txt = myupdatefcn(empt, event_obj)
pos1 = get(event_obj,'Position');
txt = {['X position ',num2str(pos1(1))],...
['Y position: ',num2str(pos1(2))]};
disp(txt);
pos_counter = pos_counter + 1; %Increase counter.
pos_x(pos_counter) = pos1(1); %Store position
pos_y(pos_counter) = pos1(2);
end
end
我希望我没听错,很难理解你在问什么......
更新评论:
更新代码:
clc
clear
close all
a=3;
b=7;
x0=0;
y0=0;
t=-pi:0.01:pi;
x=x0+a*cos(t);
y=y0+b*sin(t);
sz = 5;
scatter(x,y,sz);
[pos_x, pos_y] = ellip();
%Display position of the two selected coordinates.
fprintf('pos1 = (%f, %f), pos2 = (%f, %f)\n', pos_x(1), pos_y(1), pos_x(2), pos_y(2));
function [pos_x, pos_y] = ellip()
%Parent function (use a function instead of a script,
%allows sharing variables with internal function).
dcm_obj = datacursormode;
dcm_obj.Enable = 'on';
set(dcm_obj,'UpdateFcn',@myupdatefcn)
h = dcm_obj.Figure; %Get hendle to the figure.
hScatter = findobj(h, 'Type', 'scatter'); %Handle to scatter plot
%pos_x, pos_y and pos_counter are known both in scope of myupdatefcn and in ellip
pos_x = zeros(1,2);
pos_y = zeros(1,2);
pos_counter = 0;
%Loop until two points are selected
while (pos_counter < 2) && isvalid(h)
pause(0.01);
end
% if isvalid(h)
% close(h); %Close the figure;
% end
%dcm_obj.delete() %Delete the object (cleanup)
if isvalid(h)
dcm_obj.Enable = 'off';
set(dcm_obj,'UpdateFcn',[]) %Replace the callback with an empty function instead of deleting the object
end
%myupdatefcn is a nested function, inside ellip function
function txt = myupdatefcn(empt, event_obj)
pos1 = get(event_obj,'Position');
txt = {['X position ',num2str(pos1(1))],...
['Y position: ',num2str(pos1(2))]};
if (pos_counter > 0) && (pos_x(pos_counter) == pos1(1)) && (pos_y(pos_counter) == pos1(2))
return %Return if pod1 is already selected.
end
disp(txt);
pos_counter = pos_counter + 1; %Increase counter.
pos_x(pos_counter) = pos1(1); %Store position
pos_y(pos_counter) = pos1(2);
%Try keeping the tool-tip data
hdtip = dcm_obj.createDatatip(hScatter);
set(hdtip, 'MarkerSize',5, 'MarkerFaceColor','none', 'MarkerEdgeColor','r', 'Marker','o', 'HitTest','off');
end
end