Callback function not forwarding the variables.

1 次查看(过去 30 天)
I am developing a tool that simulates the function of a LiDAR given certain parameters. The simulation results in a plot (2D for now) that shows the pattern of the laser as well as the points captured on a given surface. I would like to be able to draw a rectangle (for now) on the 2D graph and be able to compute the number of points within the box. I have displayed a pushbutton on the graph that lets the user to start drawing the box if pressed.
The code allows the user to draw a box on the graph using ginput(2). But I am stuck in getting the number of points within the box. I am working with inpolygon() but getting errors. I appreaciate your help.
X = laser1_pts(:,1); % x - coordinates of all the points --> assign to X
Y = laser1_pts(:,2); % y - coordinates of all the points --> assign to Y
Z = laser1_pts(:,3); % z - coordinates of all the points --> assign to Z
scatter(X,Y,ptSz,'r','filled');
%% Create button to trigger polygon selection to eventually compute point density within polygon
hb = uicontrol('Style','togglebutton'); %% Create a toggle button
set(hb,'position',[1 1,120,20]) %% Setting position of the button [1PixelRight 1PixelUp, width, height]
set(hb,'String', 'Pt Density') %% Add text on the button
set(hb, 'callback', {@computeDensity,X,Y})
function computeDensity(hb,eventdata, handles,X,Y)
%polygon = zeros(4,2);
while get(hb,'Value')
[plyX, plyY] = ginput(2);
width = abs(plyX(1) - plyX(2));
height = abs(plyY(1) - plyY(2));
h = rectangle('Position',[plyX(1) plyY(1) width height]);
[in,on] = inpolygon(X,Y,plyX,plyY);
inon = in | on;
idx = find(inon(:));
xcoord = X(idx);
ycoord = Y(idx);
PointsInSelection = numel(xcoord);
pause(5);
delete(h);
end
  4 个评论
Adam
Adam 2019-11-14
Well, in your case since it is a programmatic GUI you would need to initialise something like
guidata( hb, struct );
after creating the uicontrol to create a structure in the first place. This is only useful if you wish the callback to return values.
Then
handles = guidata( hb );
would retrieve this structure within your callback.
guidata( hb, handles )
would then write the structure back into hb, which is obviously only useful if you have actually changed the struct in some way, such as adding or editing a field.
Adil Ali
Adil Ali 2019-11-14
Could you paste these commands in my code so I have a better understanding of what you are saying? I tried including your commands according to my understanding but it doesnt seem to be working.

请先登录,再进行评论。

回答(1 个)

Stijn Haenen
Stijn Haenen 2019-11-14
If you have the edges of the square you can maybe use this example to get the number of points inside the square.
x=1:100;
y=100:-1:1;
scatter(x,y)
xmin=20;
xmax=50;
ymin=70;
ymax=80;
points_x=x(x<xmax&x>xmin&y>ymin&y<ymax);
points_y=y(x<xmax&x>xmin&y>ymin&y<ymax);
num_points=numel(points_x);
hold on
scatter(points_x,points_y,'r')

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by