Creating a row vector of values from for loop
Show older comments
Im trying to create a 1x196 row vector to correspond to displacement values calculated within a for loop. I keep getting the same 10 values rather than all 196.
Code:
file = imread("C:\Users\jrtow\OneDrive\Documents\MATLAB\EN2106_Coursework_Datasets_updated\Dataset2\150617_100kA_C001H001S0001000040.tif");
B1 = 33 ; %Maximum brightness pixel located at (33,112)
A = zeros(1,196); %Row vector 1x196 to replace with displacement values
for i= 1:195
if i<10
filename = ['C:\Users\jrtow\OneDrive\Documents\MATLAB\EN2106_Coursework_Datasets_updated\Dataset2\150617_100kA_C001H001S0001000' num2str(i) '40.tif' ];%Changes file name incrimentally until the 9th picture
file = imread(filename);
maxBrightness = max(file(:));
[rowImage, columnImage] = find(file == maxBrightness);%Finds the possition of the brightest pixel
D = max(rowImage(:));
Displacement(i) = D - B1
%Finds the displacement in pixels between first and following images
elseif i<=10 && i<100
filename = ['C:\Users\jrtow\OneDrive\Documents\MATLAB\EN2106_Coursework_Datasets_updated\Dataset2\150617_100kA_C001H001S000100' num2str(i) '40.tif'];%Changes file name incrimentally for the remaining pictures
file = imread(filename);
maxBrightness = max(file(:));
[rowImage, columnImage] = find(file == maxBrightness);
D = max(rowImage(:));
Displacement = D - B1
end
end
Answers (1)
There are a few problems.
1) You should allocate Displacement prior to the i-loop just like you did for variable "A".
Displacement = zeros(1,196);
2) The "elseif" section of your conditional doesn't make much sence since if i<=10, it's also <= 100 and the first part of the conditional also looks for values less than 10. So that should be cleaned up.
3) The reason the "Displacement" variable isn't correct is because you forgot to index i in the elseif section of the conditional. 'Displacement' is correctly filled for the first 10 iterations and then it's overwritten for all subsequent iterations.
Displacement = D - B1
% Should be
Displacement(i) = D - B1
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!