How to take value from function and put it into disp script?

2 vues (au cours des 30 derniers jours)
Anne Nguyen
Anne Nguyen le 28 Sep 2019
Commenté : Star Strider le 28 Sep 2019
My task is to "display the words: 'The ball hits the ground at a distance of X meters', where X is the hitDistance from the function throwBallFunc." (I have written the throwBallFunc).
So far, I have disp( ' The ball hits the ground at a distance of %d meters '); which is not working. How do I write it so that I can get the value of X aka hitDistance from the function throwBallFunc that I created? Thank you!

Réponse acceptée

Star Strider
Star Strider le 28 Sep 2019
Use:
sprintf( ' The ball hits the ground at a distance of %d meters ',X)
or:
fprintf( ' The ball hits the ground at a distance of %d meters ', X)
instead, depending on what you want to do.
  2 commentaires
Anne Nguyen
Anne Nguyen le 28 Sep 2019
I have to use the disp function. Any way I can do it with that?
Star Strider
Star Strider le 28 Sep 2019
Yopu didn’t say that.
Try this:
disp([' The ball hits the ground at a distance of ' num2str(X) ' meters '])
That worked when I tested it. It uses the MATLAB concatenation operator [] to create one character array from three character arrays.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by