How to plot for loop with coordinates data?
Ältere Kommentare anzeigen
Hello all, I have a multiple coordinate data for 1 rectangle with format: x = [xmin1 xmax1 xmax1 xmin1 xmin1]; y = [ymin1 ymin1 ymax1 ymax1 ymin1]; the coordinates data are from (x1,y1) to (x120,y120). How I can plot the all coordinates data in one figure with for loop?
Antworten (1)
Image Analyst
am 2 Jul. 2018
I don't know why you want a for loop - perhaps it's homework??? If you require a for loop then this should work
for k = 1 : length(x)
plot(x(k), y(k), 'b*');
hold on;
end
Of course almost everyone else in the world would just plot it in one single call to plot().
plot(x, y, 'b*-');
4 Kommentare
Daniel Simarmata
am 2 Jul. 2018
Bearbeitet: Daniel Simarmata
am 2 Jul. 2018
Image Analyst
am 2 Jul. 2018
No, no for loop if you chose the very unwise way of using different variable names for each variable. (Actually yes there is a way but probably no one will tell you because it's not recommended.) It's best that you just try to put your values into an array, like xmin is an array of 120 elements, etc.
Daniel Simarmata
am 2 Jul. 2018
Bearbeitet: Daniel Simarmata
am 2 Jul. 2018
Image Analyst
am 2 Jul. 2018
Here's one way to do it. Try this little demo:
grayImage = imread('cameraman.tif');
imshow(grayImage, []);
numBoxes = 120;
x = zeros(numBoxes, 5);
y = zeros(numBoxes, 5);
numBoxesDrawn = 0;
for k = 1 : numBoxes
promptMessage = sprintf('Do you want to draw another box?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if contains(buttonText, 'No')
break;
end
k2 = 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(k, :) = round([p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)])
y(k, :) = round([p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)])
hold on
axis manual
plot(x(k, :),y(k, :))
numBoxesDrawn = numBoxesDrawn + 1;
end
% Crop array if they quit early
if numBoxesDrawn < numBoxes
x = x(1:numBoxesDrawn, :)
y = y(1:numBoxesDrawn, :)
end
Kategorien
Mehr zu Image Arithmetic finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!