How do I print unknown values

11 vues (au cours des 30 derniers jours)
Kunal Kumar
Kunal Kumar le 22 Mar 2020
Commenté : Kunal Kumar le 22 Mar 2020
Hi,
I am writing a code which asks the user for the length for a vector and then asks for the numbers they wish to input in the vector. For example if the user says its length is 3, it asks for 3 numbers that can be put inside the vector. I can display this normally as:
fprintf("Vector A is: [%.3f, %.3f, %.3f]\n", vectorA(1), vectorA(2), vectorA(3));
however, if the user enters another number I wont be able to print the values as required. Is there some type of code which can help with fixing this issue?
This is my code if it helps:
sizeofvectorA = input("How many numbers do you want to enter? ");
vectorA = [];
for i = 1:sizeofvectorA
vectorA(i) = input("Please enter a number for vector A: ");
end
fprintf("Vector A is: [%.3f, %.3f, %.3f]\n", vectorA(1), vectorA(2), vectorA(3));
thanks
  2 commentaires
James Heald
James Heald le 22 Mar 2020
i'm not sure I understand what you mean by 'if the user enters another number', can you explain
the cyclist
the cyclist le 22 Mar 2020
I believe he means that his current solution for printing is hard-coded to three values, and he wants a more general solution.

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 22 Mar 2020
vectorA = [2 3 5 7];
['Vector A is: [',sprintf('%6.3f', vectorA),']']
  1 commentaire
Kunal Kumar
Kunal Kumar le 22 Mar 2020
That works, thank you so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by