Displaying data side by side using fprintf

99 views (last 30 days)
Two questions in one day, I must be pretty bad at this. Anyways after getting past my first problem I now have to display two arrays side by side like a table. Normally I'd just combine them and print them out at once or something but the different columns have to be displayed with different significant figures.
I've tried this:
fprintf('%.2f %.3f\n', velocity,distance)
but it turns out way wrong.
The two vectors are:
velocity
170
290
-130
-70
-185
-220
200
290
270
200
300
-30
650
150
500
920
450
500
500
960
500
850
800
1090
and distance
0.0320
0.0340
0.2140
0.2630
0.2750
0.2750
0.4500
0.5000
0.5000
0.6300
0.8000
0.9000
0.9000
0.9000
0.9000
1.0000
1.1000
1.1000
1.4000
1.7000
2.0000
2.0000
2.0000
2.0000
All I need is a way to get them next to each other. I can figure out the significant figures and spacing myself.
Thanks alot.

Accepted Answer

the cyclist
the cyclist on 21 Mar 2013
One way:
fprintf('%8.2f %8.3f\n', [velocity,distance]')
  4 Comments
Shahbaz BHATI
Shahbaz BHATI on 22 Oct 2018
is there any way i can add serial numbers to the same output?
the cyclist
the cyclist on 22 Oct 2018
If your serial number is purely numeric, then you can just add another column:
velocity = [2.011;3.012;4.013];
distance = [2.001;3.002;4.003];
sn = [487969816; 487969876; 487969873];
sprintf('%10d %8.2f %8.3f\n', [sn,velocity,distance]')
If not (e.g. if your serial number is something like SN4876), then you'll need a different approach. I suggest you open a new question with a bit more detail and a small sample of what your input data look like. (If you link that new question here, I'll try to look for it.)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!