Effacer les filtres
Effacer les filtres

How do I print the following for loop as A1, A2, A3, A4?

6 vues (au cours des 30 derniers jours)
Kartik Bharadwaj
Kartik Bharadwaj le 30 Août 2017
Commenté : Stephen23 le 30 Août 2017
for i = 1:4
print A%d, i
end

Réponse acceptée

Star Strider
Star Strider le 30 Août 2017
Use fprintf or sprintf.
Try this:
for i = 1:4
fprintf('A%d\n', i)
end

Plus de réponses (2)

Jan
Jan le 30 Août 2017
Modifié(e) : Jan le 30 Août 2017
for i = 1:4
fprintf('A%d, ', i);
end
Or without a loop:
fprintf('A%d, ', 1:4);
Or omit the last comma:
fprintf('A%d, A%d, A%d, A%d\n', 1:4);
Do not forget:
disp('A1, A2, A3, A4')
;-)

Walter Roberson
Walter Roberson le 30 Août 2017
Modifié(e) : Walter Roberson le 30 Août 2017
cellstr( num2str( (1:4).', 'A%d') )
If you have R2017a or later, then consider
"A" + (1:4).'
If you have R2016b, then consider
string('A') + (1:4).'

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by