How to plot for loop with coordinates data?

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
Image Analyst am 2 Jul. 2018

0 Stimmen

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
Daniel Simarmata am 2 Jul. 2018
Bearbeitet: Daniel Simarmata am 2 Jul. 2018
I mean like this:
x = [xmin1 xmax1 xmax1 xmin1 xmin1];
y = [ymin1 ymin1 ymax1 ymax1 ymin1];
figure; plot (x,y);
hold on;
x = [xmin2 xmax2 xmax2 xmin2 xmin2];
y = [ymin2 ymin2 ymax2 ymax2 ymin2];
plot(x,y);
hold on;
x = [xmin3 xmax3 xmax3 xmin3 xmin3];
y = [ymin3 ymin3 ymax3 ymax3 ymin3];
plot(x,y);
hold on;
x = [xmin4 xmax4 xmax4 xmin4 xmin4];
y = [ymin4 ymin4 ymax4 ymax4 ymin4];
plot(x,y);
hold on;
So the above code are an example (from 1 to 4 coordinates data) generated by manual, which mean I have to type each (x,y) manually until 120 coordinates data. The question is whether there is a way that can be done automatically by using a for loop?
Image Analyst
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
Daniel Simarmata am 2 Jul. 2018
Bearbeitet: Daniel Simarmata am 2 Jul. 2018
Yes I know, because the variable which is a rectangle coordinates (based on 4 point xmin,ymin,xmax,ymax) have to be defined manually. So based on 120 coordinates point of rectangle that I have already defined, I have to plot all the 120 rectangles with the way that previously I explained. Thanks for your advice, may be I must defined the 120 rectangles coordinate in a array first.
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

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 2 Jul. 2018

Kommentiert:

am 2 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by