How can I save the coordinates from ginput()?

조회 수: 8 (최근 30일)
Jake
Jake 2013년 5월 12일
I want to get the bounding boxes of two buttons on a screen.
I would like to write a loop with ginput such that for each button, I want to get input (ginput) from the mouse for four points, which will form the bounding box for the buttons. Something like:
for 1:3; %For 3 buttons
[x,y] = ginput(4) %Get the mouse input from 4 clicks (This forms the bouding box)
end
But this code is wrong because the third iteration will overwrite the coordinates that I got from the first and second iterations.
Also, I'd like to save these so that if I type in x(2), I get all the x coordinates for the second button. If I type in y(2), I get all the y coordinates for the second button.
Thank you!
  댓글 수: 1
Jake
Jake 2013년 5월 12일
Sorry, I want to get the bounding boxes of THREE buttons on a screen, or picutre

댓글을 달려면 로그인하십시오.

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 12일
Use a cellarray
for k=1:3; %For 3 buttons
[ii,jj] = ginput(4)
x{k}=ii
y{k}=jj
end
  댓글 수: 2
Jake
Jake 2013년 5월 12일
This does not work. It gives me the first x value, then for the second iteration it gives me the first x value and the 2nd x value. Same for y.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 12일
No, it works. Check the result
cell2mat(x)
cell2mat(y)

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2013년 5월 12일
Why not just use rbbox()??? That would be the more sensible way to draw a box around something than calling ginput(4). Here's the demo from the help:
uiwait(msgbox('Draw the box'));
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)]
y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)]
hold on
axis manual
plot(x,y)

카테고리

Help CenterFile Exchange에서 Data Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by