array values are overwritten
이전 댓글 표시
Hello,
I am working with for loop in my code. I am getting the answer but after 8 steps i.e. from the step 9, the value of array is overwritten.
means The value I was getting at column 2 in the first 8 steps , I am getting that value in the column 5 when i start with step 9.
please help me for the same.
댓글 수: 16
KALYAN ACHARJYA
2019년 7월 19일
편집: KALYAN ACHARJYA
2019년 7월 19일
Please share the code, if possible?
Nicolas B.
2019년 7월 19일
Do you have any example code to illustrate your problem?
swati mane
2019년 7월 19일
편집: Jan
2019년 7월 19일
madhan ravi
2019년 7월 19일
Swati learn to format your code properly by selecting the code and pressing the code button.
Jan
2019년 7월 19일
@swati mane: Please post explicitly, which variables are affected. Column 2 of what?
A simplification of your code:
imgFiles = dir('*.jpg');
numFiles = numel(imgFiles); % NUMEL is safer than LENGTH
valueOfA = zeros(1, numFiles);
for k = 1:numFiles - 1
img = imread(imgFiles(k).name);
compareimg = imread(imgFiles(k + 1).name);
P = imabsdiff(img,compareimg);
figure;
imshow(P,[]);
valueOfA(k) = nnz(P);
end
swati mane
2019년 7월 19일
편집: swati mane
2019년 7월 19일
Joel Handy
2019년 7월 19일
편집: Joel Handy
2019년 7월 19일
It sounds like maybe you are getting the same or simiiar output, just the order of the values is changing.
Look at the order of imgFiles when you run with 8 images and when you run with 9 images. My guess is that the order of the file list that the call to dir gives you changes. What are the names of these image files?
swati mane
2019년 7월 19일
swati mane
2019년 7월 19일
Joel Handy
2019년 7월 19일
Because the file list returned by "dir" is sorted more or less alphabetically (it does capital letters before lower case ones). If I have 9 images:Img1.jpg, Img2.jpg, Img3.jpg, etc, dir will return:
Img1.jpg, Img2.jpg, Img3.jpg, Img4.jpg, Img5.jpg, Img6.jpg, Img7.jpg, Img8.jpg, Img9.jpg
If I add an new file with name Img10.jpg, it will return:
Img1.jpg, Img10.jpg, Img2.jpg, Img3.jpg, Img4.jpg, Img5.jpg, Img6.jpg, Img7.jpg, Img8.jpg, Img9.jpg
swati mane
2019년 7월 19일
편집: swati mane
2019년 7월 19일
swati mane
2019년 7월 19일
Joel Handy
2019년 7월 19일
편집: Joel Handy
2019년 7월 19일
One option is to rename your files. If you named them something like Img001, img002, .., Img498, Img499, Img500, dir would return your file names in the right order.
You might be able to sort your images by date. The file info that dir gives you has the files' datestamp. That can be unreliable though if people are copying images around or opening and closing them. Anything that would update the files metadata.
The most reliable but most complicated option would be to parse you file names to extract the file number and sort that way.
imgFiles=dir('*.jpg');
fileNames = {imgFiles.name}'
fileNumStr = regexp(fileNames, '[0-9]*', 'match');
fileNum = str2double([fileNumStr{:}]);
[~,sortIdx] = sort(fileNum);
imgFiles = imgFiles(sortIdx);
swati mane
2019년 7월 19일
swati mane
2019년 7월 24일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!