Printing lines of text from cell array?

8 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by