# fprintf columns turning out weirdly

8 views (last 30 days)
Liam Crocker on 28 Sep 2020
Edited: Stephen23 on 28 Sep 2020
So I've got some sets of data and their all columns, such as the example below. As obvious x and y both become column matrices of 11 x 1. I try and get these to print like x(1) y(1)
x(2) y(2)
Which I can
Attempt 4 works for this short example, but with my problem which has lots more values and such it doesnt work, it still prints the values out horizontally. With my problem in my code with lots of data (which I will attatch) it prints out the values, semi correctly, but in a weird kind of way. I am printing some time values then some fitted time values, repeat this 4 times to have a total of 8 arrays of values I am trying to print. So thats why it will go .165 .165. It's all fine up untill the last two columns, the tjohn1 and tf4 (tf4 is the fitted time for john). It prints the intial values of these two columns underneath the first two columns and so the data when printed looks all funky.
Why would It print like this, it's like it's running out of horizontal space in the window and so it jams it underneath the first two columns and how would I fix this?
x = 0:1:10;
x = x'
y = 1:1:11;
y = y'
z =
fprintf('%2.2f | %2.2f \n', [x'; y';]) %This works for this small example
0.00 | 1.00
1.00 | 2.00
2.00 | 3.00
3.00 | 4.00
4.00 | 5.00
5.00 | 6.00
6.00 | 7.00
7.00 | 8.00
8.00 | 9.00
9.00 | 10.00
10.00 | 11.00
%However in my coding, it prints out like this
0.165 0.165 0.142 0.142 0.193 0.193
0.129 0.129 1.850 1.825 1.890 1.791
1.940 1.878 1.840 1.810 2.870 2.793
2.880 2.751 2.960 2.866 2.860 2.795
3.780 3.687 3.780 3.637 3.910 3.780
3.800 3.705 4.650 4.555 4.640 4.497
4.780 4.669 4.670 4.590 5.500 5.415
5.470 5.348 5.640 5.549 5.530 5.467
6.320 6.271 6.290 6.195 6.500 6.425
6.380 6.340 7.140 7.125 7.100 7.041
7.360 7.301 7.230 7.212 7.960 7.980
7.920 7.887 8.220 8.177 8.100 8.084
8.790 8.835 8.750 8.733 9.070 9.053
8.960 8.957 9.690 9.690 9.580 9.580
9.930 9.930 9.830 9.830 %as seen, the two .129's are jammed under the first two columns, where I want them beside the .193 columns
Liam Crocker on 28 Sep 2020
Liam Crocker on 28 Sep 2020
I attatched the data file and also a screenshot of what it looks like.

Stephen23 on 28 Sep 2020
Edited: Stephen23 on 28 Sep 2020
"Why would It print like this..."
Because that is exactly what you told fprintf to do: your fprintf format string has six format specifiers and then a newline. So fprintf prints six values and then a newline. Just like you told it to.
Note that the number of columns or rows in the matrix is totally irrelevant, fprintf simply continues to eat-up the elements from the matrix (going down each column in turn) until there are no more elements to eat. How many elements there are in each column/row/page/... is irrelevant.
"...and how would I fix this?"
Simply change the format string to have eight format specifiers.
What you did:
fprintf('%2.3f | %2.3f | %2.3f | %2.3f | %2.3f | %2.3f\n',...)
What you need to do:
fprintf('%2.3f | %2.3f | %2.3f | %2.3f | %2.3f | %2.3f | %2.3f | %2.3f\n',...)
Or the lazy-programmer way (which is also much more versatile):
mat = [tbolt1(:),tf1(:),tbolt2(:),tf2(:),tlewis1(:),tf3(:),tjohn1(:),tf4(:)];
fmt = repmat(' | %2.3f',1,size(mat,2));
fprint(fmt(4:end),mat.')
Note that you could significantly simplify your code by not forcing meta-data (i.e. runners' names) into the variable names and simply using arrays/matrices right from the start. This would avoid all of that duplicated code.
Liam Crocker on 28 Sep 2020
Oh I am a complete fool, so sorry to bother you with that question Stepehen!!

### Categories

Find more on Characters and Strings in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!