How to keep for loop from over writing variables?
22 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Adam
2019-3-15
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)) )
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!