Printing lines of text from cell array?

8 vues (au cours des 30 derniers jours)
Tyler Bodnarik
Tyler Bodnarik le 16 Nov 2020
Commenté : Star Strider le 17 Nov 2020
Suppose I had a simple cell array :
D = {[1 2 3],[4 5 6 7 8 9],[10 11 12 13 14 15 16 17 18 19 20]}
How could I print each line (one cell per line) into the command window, one character at a time?
I know fprintf('\n') needs to be used to jump to the next line.
Appreciate any advice.

Réponse acceptée

Star Strider
Star Strider le 16 Nov 2020
One approach:
D = {[1 2 3],[4 5 6 7 8 9],[10 11 12 13 14 15 16 17 18 19 20]}
for k = 1:numel(D)
fprintf(1, [repmat('%d ',1,numel(D{k})) '\n'],D{k})
end
.
  2 commentaires
Tyler Bodnarik
Tyler Bodnarik le 17 Nov 2020
Would that output one character at a time? I tried it but couldn't tell. I may have to use pause somewhere.
Star Strider
Star Strider le 17 Nov 2020
It outputs a line at a time.
To output one character at a time, a second loop that loops through each line would be necessary:
D = {[1 2 3],[4 5 6 7 8 9],[10 11 12 13 14 15 16 17 18 19 20]}
for k1 = 1:numel(D)
for k2 = 1:numel(D{k1})
fprintf(1, '%d ',D{k1}(1,k2))
end
fprintf('\n')
end
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by