How to reset the size variable inside looping?
Ältere Kommentare anzeigen
I have a loop, but the variable keep continuing. How could I set null variable without use clear or clear all
Example :
file_pattern = fullfile(folder,'truntum*.*');
thefile = dir(file_pattern);
% Access each image file
for k = 1 : length(thefile)
baseFilename = thefile(k).name;
fullFilename = fullfile(folder, baseFilename);
picture = imread(fullFilename);
BW = im2bw(picture);
% Process the picture
% Result : some detected object
% For example, 14 object detected
area(totalObject,1) = 0;
% Save the area of each object
% Then access some index, example area(2,1) area(3,1) area(7,1)
end
Variable area size is array 14 x 1. For the second looping (k=2), with different image, the total object detected is different. So, the size of variable area is different too. For the example, k=2 with totalObject = 7. It's less than the lastest total object. But, the size of area is still 14x1 and the value in index 8-14 still inside the variable. So, in k=2 index over the total object still working to access when it should be we could not access the index because the area size is 7x1.
Any idea what should I do to solve this problem? I tried to use clear all to reset all variables in workspace. But it make the k variable reseted and disturb the looping. Do you know to reset size of variable? Thank you
Antworten (1)
Best, most robust and clearest solution: define area explicitly not implicitly:
area = zeros(totalObject,1);
clear area
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!