# How to use if statement inside a for loop?

1 view (last 30 days)
Emily on 5 May 2022
Commented: Walter Roberson on 6 May 2022
I'm currently trying to print out something if the difference between the plots is bigger than 100.
for i = 2:10
plot(x(:,25+i)
dif(:,i) = x(:,25+i) - x(:,25+(i-1)); %error here
if dif >200
fprintf( ) %print out the x value where it was triggered
else
fprintf('nothing')
end
I'm having trouble saving each occurance that it happens.

Walter Roberson on 5 May 2022
for i = 2:10
plot(x(:,25+i)) ; hold on
dif(:,i) = x(: ,25+i) - x(:,25+(i-1));
mask = dif(:, i) > 200
fprintf('col %d: %s\n', 25+i, mat2str(x(mask, 25+i))); %print out the x value where it was triggered
else
fprintf('col %d: nothing\n');
end
end
Walter Roberson on 6 May 2022
The code I showed cannot print out the y axis values. The code I showed uses mat2str(x(mask, 25+i)) which displays x values, not y values.
The vectorized form of the code would look more like
i = 2:10;
plot(x(25+i))
dif = x(:,25+i) - x(:,25+i-1);
for c = 1:size(dif,2)
fprintf('col %d: %s\n', 25+c+1, mat2str(x(submask, 25+c+1))); %print out the x value where it was triggered
else
fprintf('col %d: nothing\n', 25+c+1);
end
end