How to keep for loop from over writing variables?

I am new to MATLAB and teaching myself how to use it. I am trying to run some image processing code to measure the vertical length of objects/blobs in an image. I am also calculating the width so that I can later classify the blobs. I need to run this for hundreds of images once it is finished and ideally export all data to an excel (or similar) file fo analysis. I am stuck in my for loops. They keep the last variable but I would like it to keep all data (i.e. values for each blob). How can I keep the for loop from over writing each variable? Below is my code and attached are example post processed images. Any help is greatly appreciated!
close all
file=uipickfiles;
for a = 1:length(file)
RGB=imread(file{a});
imshow(RGB);
label = bwlabel(BW5);
max(max(label));
%Calculating length and width of the blobs;
for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
width=max(col)-min(col)+2;
end
end
filename = 'testexcel.xlsx';
xlswrite(filename,len);
winopen('testexcel.xlsx');

10 Kommentare

Stephen23
Stephen23 am 14 Mär. 2019
Bearbeitet: Stephen23 am 14 Mär. 2019
"I am new to MATLAB and teaching myself how to use it"
Good idea! You should start by doing the introductory tutorials, which teach many basic MATLAB concepts that you will need to know:
"How can I keep the for loop from over writing each variable?"
Use indexing, exactly as the introductory tutorials show (along with many other basic concepts that you will need to know in order to use MATLAB),
IowaClassic
IowaClassic am 14 Mär. 2019
Thank you Stephen Cobeldick. I have looked through all these tutorials and many others. I cannot seem to get the loop from overwriting. I have tried cell arrays and zero arrays, but they do not seem oto work. I still end up with just one variable for each length and width. I am not doing something right. I am not experienced enought to see what that is.
Would you post your updated code so we can check your indexing, please?
Bob, here is my latest attempt. TIA
label = bwlabel(BW5);
max(max(label));
%Calculating length and width of the blobs;
for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
width=max(col)-min(col)+2;
target = zeros(unit8,[len width]);
end
I also tried
for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
width=max(col)-min(col)+2;
target = zeros(len,double);
target2 = zeros(width,double);
end
You aren't indexing anything on the left-hand side of all those lines, which is the side where things are stored/overwritten. e.g.
len( j ) = max(row) - min(row) + 2;
IowaClassic
IowaClassic am 14 Mär. 2019
Adam, you just solved the first problem!! I can see the length for all blobs in the image under each variable. When i run multiple images it rewrites varlable len and width. Is it possible to add to this matrix rather than overwrite it? TIA
Stephen23
Stephen23 am 14 Mär. 2019
Bearbeitet: Stephen23 am 14 Mär. 2019
"I have looked through all these tutorials and many others"
If you "looked through all these tutorials" then you will have seen the section "Loops and Conditional Statements" on this page
and seen that every loop example uses indexing into the output variable, e.g.:
for k = 1:nsamples
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData);
end % ^^^ indexing into the output variable!
You should adapt this for your code.
Well if you have an outer loop running round some number of images you would need a 2d output result and indexing as e.g.
for n = 1:numImages
for j = 1:...
len( n, j ) = ...
width( n, j ) = ...
...
end
end
You should also presize these variables outside the outer loop as e.g.
len = zeros( numImages, max(max(label)) )
IowaClassic
IowaClassic am 14 Mär. 2019
Bearbeitet: IowaClassic am 14 Mär. 2019
Thanks, Adam. When i presize outside the outerloop, I get an undefned function error. Presizing before the inner loop eliminates this. However, it returns the first three lines of the matrix as zeros. I believe this happen because it is getting resized for each image but only has data for the image it is currently processing. How do I presize outside the outer loop without the undefined function error? Thank you
close all
clear all
file=uipickfiles;
numImages = length(file);
for a = 1:numImages
RGB=imread(file{a});
%there is some image processing code here to get RGB to a binary for the measuring of blobs
imshow(RGB);
label = bwlabel(BW5);
max(max(label));
len = zeros( numImages, max(max(label)) )
width = zeros( numImages, max(max(label)) )
%Calculating length and width of the blobs;
for j=1:max(max(label))
[row, col] = find(label==j);
len(a,j)=max(row)-min(row)+2;
width(a,j)=max(col)-min(col)+2;
end
end
IowaClassic
IowaClassic am 14 Mär. 2019
All, I figured it out what I was doing wrong. Thank you for your help!
Adam, Please add your answer to the answer section so that I can mark it as the accepted answer.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Adam
Adam am 15 Mär. 2019
You aren't indexing anything on the left-hand side of all those lines, which is the side where things are stored/overwritten. e.g.
len( j ) = max(row) - min(row) + 2;
If you have an outer loop running round some number of images you would need a 2d output result and indexing as e.g.
for n = 1:numImages
for j = 1:...
len( n, j ) = ...
width( n, j ) = ...
...
end
end
You should also presize these variables outside the outer loop as e.g.
len = zeros( numImages, max(max(label)) )

Weitere Antworten (0)

Gefragt:

am 14 Mär. 2019

Beantwortet:

am 15 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by