I want to display rectangle to every image in the loop but my code shows rectangle only at last image.So please help me

2 views (last 30 days)
here is my code
if true
% code
for k = 100 : 108
i=i+1;
I=imread(['C:\Users\shraddha\Documents\MATLAB\Snaps\' num2str(k) '.png']);
imshow(I);
subplot(3,3,i)
a=rand(4);
rectangle('Position',a(1,:),'linewidth',1,'linestyle','-','EdgeColor','r');
end end
here rectangle function shows rectangle at last plot,but i want it to every frame in the subplot... please help me..

Answers (1)

Image Analyst
Image Analyst on 24 Nov 2014
Put this before the end of the loop
drawnow;
pause(0.5);
  1 Comment
Image Analyst
Image Analyst on 24 Nov 2014
There's so much wrong with it that I just decided to fix everything. The main problem was that the subplot was not before the imshow() and rectangle() - it was between them. Here is the fixed code, which has a number of improvements:
clc;
fontSize = 10;
plotNumber = 0;
folder = 'C:/Users/shraddha/Documents/MATLAB/Snaps';
for k = 100 : 108
plotNumber=plotNumber+1;
subplot(3, 3, plotNumber);
if plotNumber == 1
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
end
baseFileName = sprintf('%d.png', k);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
thisImage = imread(fullFileName);
imshow(thisImage);
axis on;
title(baseFileName, 'FontSize', fontSize, 'Interpreter', 'none');
recPosition = rand(1, 4);
hold on;
rectangle('Position', recPosition,...
'linewidth',1,...
'linestyle','-',...
'EdgeColor','r');
else
message = sprintf('Error: the image file does not exist:\n%s', fullFileName);
title(message, 'FontSize', fontSize, 'Interpreter', 'none');
uiwait(warndlg(message));
end
end

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!