Why does a for loop 1:1:n index at a different rate than +1?
Show older comments
Hello, I am reading the first couple values from a data file, but I noticed that values in my for loop were not matching the values in the stored in my data file. Speficially the second loop which displays both the value of i and the of Z.
My questions are:
1: Why does the second for loop behaive differently than the first; shouldn't i always increment by whole number values?
2. Why does the second for loop contain more than 5 iterations?
3. Why are the values of Z in the second for loop different than what is actually store in them? The values from Z at the end give the correct values from the Data csv file.
Thank you for any information you can provide.
The code:
% Goal: Read data from Shipmotion CSV file
clc;
Data = csvread('RegularShipMotion_Hs2_Tp15_Vs15_HG0.csv', 1, 0, 'A37..G65037');
%number of data points (rows) from datafile
n = 5;
%Column A: Time
%Column D: Z
% Time (t)
t = Data(2:n,[2]);
X = Data(2:n,[3]); %Nonzero for RegularShip...
Y = Data(2:n,[4]);
Z = Data(2:n,[5]); %Nonzero for RegularShip...
Pitch = Data(2:n,[6]);
Roll = Data(2:n,[7]); %Nonzero for RegularShip...
Yaw = Data(2:n,[8]);
%Loop testing
fprintf("\nRunning...")
for i=1:1:n
fprintf("the value of i is %f \n", i)
end
fprintf("\n")
for i=1:1:n
fprintf("the value of i is %f and the value of z is %f \n", i, Z)
end
fprintf("\n")
Z
1 Comment
Cameron Eggart
on 15 Feb 2022
Accepted Answer
More Answers (0)
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!