Why does a for loop 1:1:n index at a different rate than +1?

3 views (last 30 days)
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
Cameron Eggart on 15 Feb 2022
My output is this:
the value of i is 1.000000
the value of i is 2.000000
the value of i is 3.000000
the value of i is 4.000000
the value of i is 5.000000
the value of i is 1.000000 and the value of z is 3.015000
the value of i is 3.005000 and the value of z is 2.995000
the value of i is 2.982000 and the value of z is the value of i is 2.000000 and the value of z is 3.015000
the value of i is 3.005000 and the value of z is 2.995000
the value of i is 2.982000 and the value of z is the value of i is 3.000000 and the value of z is 3.015000
the value of i is 3.005000 and the value of z is 2.995000
the value of i is 2.982000 and the value of z is the value of i is 4.000000 and the value of z is 3.015000
the value of i is 3.005000 and the value of z is 2.995000
the value of i is 2.982000 and the value of z is the value of i is 5.000000 and the value of z is 3.015000
the value of i is 3.005000 and the value of z is 2.995000
the value of i is 2.982000 and the value of z is
Z =
3.0150
3.0050
2.9950
2.9820
>>

Sign in to comment.

Accepted Answer

Voss
Voss on 15 Feb 2022
Edited: Voss on 15 Feb 2022
Look what happens when you pass a non-scalar array to fprintf():
Z = [1; 2; 3];
fprintf('the value of Z is: %f',Z);
the value of Z is: 1.000000the value of Z is: 2.000000the value of Z is: 3.000000
The entire format string is repeated for each element. That's what's going on here, since you are sending the whole array Z to fprintf() each time. If you want to see the ith element of Z each time, you can say:
for i=1:1:n
fprintf("the value of i is %f and the value of z is %f \n", i, Z(i))
end

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!