Printing lines of text from cell array?

조회 수: 10 (최근 30일)
Tyler Bodnarik
Tyler Bodnarik 2020년 11월 16일
댓글: Star Strider 2020년 11월 17일
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.

채택된 답변

Star Strider
Star Strider 2020년 11월 16일
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
Tyler Bodnarik
Tyler Bodnarik 2020년 11월 17일
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 2020년 11월 17일
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
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by