not correct output when using fprintf with multiple lines

Something is wrong here.. I need to associate the number of students to each class.. Any suggestion?
fprintf('class %d: number of students %d\n',[1:5], [15 17 12 18 15])
class 1: number of students 2 class 3: number of students 4 class 5: number of students 15 class 17: number of students 12 class 18: number of students 15
% desired output
class 1: number of students 15
class 2: number of students 17
class 3: number of students 12
class 4: number of students 18
class 5: number of students 15

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 30 日
編集済み: Dyuman Joshi 2023 年 9 月 30 日

1 投票

Arrange the values to make a 2xn vector.
fprintf('class %d: number of students %d\n',[1:5; 15 17 12 18 15])
class 1: number of students 15 class 2: number of students 17 class 3: number of students 12 class 4: number of students 18 class 5: number of students 15
The values are used in the order they are provided.
For a single input, values are used column-wise (Indexing and storing data in MATLAB is column based). For multiple inputs, each input is treated as a single input serially/sequentially.

その他の回答 (0 件)

カテゴリ

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

質問済み:

Sim
2023 年 9 月 30 日

編集済み:

2023 年 9 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by