Callback function not forwarding the variables.

1 回表示 (過去 30 日間)
Adil Ali
Adil Ali 2019 年 11 月 14 日
編集済み: Guillaume 2019 年 11 月 14 日
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by