Problem with fprintf command (matrix)

So I have a problem with fprintf command(image attached), I'm trying to display this text and XX.XX are my variables
For a weight of XX.XX Newtons, the forces are:
Ax Ay Bx F1 F2
XX.XX XX.XX XX.XX XX.XX XX.XX
Let "ans" be 5x1 column matrix and my code is
fprintf('For a weight of %4.2f Newtons, the forces are:\nAx Ay Bx F1 F2\n%4.2g',weight,ans)
%and the answer I get is below but it's not how I'm trying to do it
For a weight of 50.00 Newtons, the forces are:
Ax Ay Bx F1 F2
-50For a weight of 50.00 Newtons, the forces are:
Ax Ay Bx F1 F2
-50For a weight of 70.71 Newtons, the forces are:
Ax Ay Bx F1 F2
-50

 Réponse acceptée

Star Strider
Star Strider le 29 Sep 2019
You need to provide separate format descriptors for each value you want to print on the same call to fprintf.
Try this:
fprintf('For a weight of %4.2f Newtons, the forces are:\nAx Ay Bx F1 F2\n%4.2g %4.2g %4.2g %4.2g %4.2g\n',weight,ans)
Experiment to get the result you want.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by