Need help using fprintf for cell array

I have a structure named 'shape' that contains three sub-structures ('circle', 'triangle', 'square') and need to print them out in a list. Here is my code:
%Create shape super-structure.
shape = struct('circle', [], 'triangle', [], 'square', []);
%Print names of created shapes.
fields = fieldnames(shape);
fprintf('You created these shapes:\n%s\n', fields{:});
I need the command window to show this... You created these shapes:
circle
triangle
square
...but instead, it prints "You created these shapes:" before every single shape. How should I go about fixing this?

1 件のコメント

Ruger28
Ruger28 2019 年 10 月 10 日
Also, please use the code formattting.

サインインしてコメントする。

 採用された回答

Ruger28
Ruger28 2019 年 10 月 10 日
編集済み: Ruger28 2019 年 10 月 10 日

0 投票

Give this a go:
shape = struct('circle', [], 'triangle', [], 'square', []);
%Print names of created shapes.
fields = fieldnames(shape);
% fprintf('You created these shapes:\n%s\n', fields{:});
fprintf('You created these shapes:\n');
fprintf('%s\n',fields{:})
You need to seperate the first line from the rest if you don't want repeats.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLinear Algebra についてさらに検索

質問済み:

2019 年 10 月 10 日

コメント済み:

2019 年 10 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by