Callback function not forwarding the variables.

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일
편집: Adam 2019년 11월 14일
You didn't say what error you are getting. At a glance though, your function is expecting handles passed in as the 3rd argument, whereas you are passing in just X and Y after the standard callback source and eventdata variables. Since you apparently don't use handles in the function anyway you might as wekk just delete it. Although you do assign values to variables in the callback which will instantly go out of scope too.
You may instead wish to use
handles = guidata( hb );
...
handles.PointsInSelection = numel( xcoord );
guidata( hb, handles )
if you want to have access to PointsInSelection outside the callback
Adil Ali
Adil Ali 2019년 11월 14일
편집: Guillaume 2019년 11월 14일
Thanks Adam. So the error was
Not enough input arguments.
Error in Single_Axis_Scnner_v1>computeDensity (line 125)
[in,on] = inpolygon(X,Y,plyX,plyY);
Error while evaluating UIControl Callback.
It went away when I removed the handles argument.
Can you let me know if
handles = guidata( hb );
be placed within the function? and whats the purpose of calling
guidata( hb, handles )
again?
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일

0 개 추천

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')

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

질문:

2019년 11월 14일

편집:

2019년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by