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?
回答 (1 件)
Image Analyst
2018 年 7 月 2 日
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 件のコメント
Daniel Simarmata
2018 年 7 月 2 日
編集済み: Daniel Simarmata
2018 年 7 月 2 日
Image Analyst
2018 年 7 月 2 日
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
2018 年 7 月 2 日
編集済み: Daniel Simarmata
2018 年 7 月 2 日
Image Analyst
2018 年 7 月 2 日
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
カテゴリ
ヘルプ センター および File Exchange で Image Arithmetic についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!