For loop doesnt terminate-kindly help

조회 수: 9 (최근 30일)
shivasakthi
shivasakthi 2017년 1월 28일
댓글: shivasakthi 2017년 1월 30일
Hi, I have written a code to apply a random crop for a set of three images stored in a folder '1' at D: drive and store it in a separate folder named 'chrom-randcrop1' in the same drive. The code is as follows:
myFolder = 'D:\1';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp');
theFiles = dir(filePattern);
mkdir('D:\chrom-randcrop1')
% image processing-random cropping of a 100*100 image from the input image
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
imageArray = imread(fullFileName);
imageArray5=random_square_grab(imageArray,100);
baseOutputFileName = sprintf('randcrop100_1(%d).bmp', k);
outputFolder = 'D:\chrom-randcrop1';
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
imwrite(imageArray5, fullOutputFileName)
end
dir D:\chrom-randcrop1
The issue is the loop doesn't terminate. The folder is created but then the loop remains busy. Kindly help me with the corrections in this code. Thanks.
  댓글 수: 1
Star Strider
Star Strider 2017년 1월 28일
What is the result of only running:
length_theFiles = length(theFiles)
return % Stop The Script (Delete Later)
before the loop.

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

채택된 답변

Image Analyst
Image Analyst 2017년 1월 28일
Add this before the for loop
whos theFiles % No semicolon!
numFiles = length(theFiles) % No semicolon!
if numFiles == 0 || isempty(theFiles)
warningMessage = 'No files found';
uiwait(warndlg(warningMessage));
return;
end
for k = 1 : numFiles
What do you see in the command window, and does the warning pop up a message box?
  댓글 수: 1
shivasakthi
shivasakthi 2017년 1월 30일
Hi, The code perfectly works now. There was no warning messages while I used your code given above. I had a problem with one of the function definitions. I corrected it and got the results. Many thanks for your assistance.

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

추가 답변 (1개)

Jan
Jan 2017년 1월 28일
The issue is the loop doesn't terminate.
Strange. What does happen instead? Does Matlab convert millions of files? Or does the code of random_square_grab contain an infinite loop? Where does Matlab stop, when you hit Ctrl-C?

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by