Printing lines of text from cell array?

8 Ansichten (letzte 30 Tage)
Tyler Bodnarik
Tyler Bodnarik am 16 Nov. 2020
Kommentiert: Star Strider am 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.

Akzeptierte Antwort

Star Strider
Star Strider am 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 Kommentare
Tyler Bodnarik
Tyler Bodnarik am 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 am 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
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by