Printing lines of text from cell array?

8 visualizaciones (últimos 30 días)
Tyler Bodnarik
Tyler Bodnarik el 16 de Nov. de 2020
Comentada: Star Strider el 17 de Nov. de 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.

Respuesta aceptada

Star Strider
Star Strider el 16 de Nov. de 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 comentarios
Tyler Bodnarik
Tyler Bodnarik el 17 de Nov. de 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 el 17 de Nov. de 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
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by