MATLAB: Hello question to iterate over elements of a column

for loopiterationmatrix

I have a CSV file, I have imported the file and have to do operations on one particular column and find the first nonzero element and the corresponding time stamp from another column. However, when I try to iterate over the required column it directly points to the last non-zero element. I do not understand the mistake in my code. Kindly help me understand the mistake. Any help appreciated. Thanks in advance.
for i = 1:1:length(zRPM)
curzRPM = zRPM(i);
if zRPM(i) ~= 0
boutstart = zRPM(i);
starttime = timestamp(i);
% for j = 1:1:length(zRPM)
% if zRPM(j) == 0
% endtime = timestamp(j-1);
% end
% end
end
output:
8.8772 <---- This is the last non-zero element in the column and its
corresponding timestamp
'09-07-2017 16:00'

Best Answer

Break out of the loop when you find that first non-zero value:
if zRPM(i) ~= 0
boutstart = zRPM(i);
starttime = timestamp(i);
break; % <-- ADDED THIS
end
You could also use the "find" function for this and avoid the loop.