MATLAB Answers

Ian Barker

Displaying data side by side using fprintf

Asked by Ian Barker
on 21 Mar 2013

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.

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by the cyclist
on 21 Mar 2013
 Accepted answer

One way:

fprintf('%8.2f %8.3f\n', [velocity,distance]')

  2 Comments

Ian Barker
on 21 Mar 2013

Ah that apostrophe at the end seems to do the trick. What does that even do exactly?

Anyways thanks a lot for your help.

the cyclist
on 22 Mar 2013

It's taking the transpose of the matrix.

doc transpose

for details.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today