printing matrix data in the correct order (fprintf)

3 ビュー (過去 30 日間)
Aya
Aya 2022 年 10 月 9 日
回答済み: Star Strider 2022 年 10 月 9 日
i would like to print this data set in the exact same format, same data on each column and row but matlab prints the rows into columns, im not sure how to print it correctly.
heres what i did:
*the variable name of the matrix below is: data_info;
data to print:
2020 3 21 12 0
2020 3 21 12 10
2020 3 21 12 20
2020 3 21 12 30
2020 3 21 12 40
2020 3 21 12 50
each column is has a corresponding heading: year,month,day,hour,minute
my code:
%transposing the data
trans_alldata = data_info';
%column headings
fprintf('YEAR\tMONTH\tDAY\tHOUR\tMINUTE\n');
%printing only the first 4 lines
fprintf('%d\t%d\t%d\t%d\t%d\n',trans_alldata(1:4,1),trans_alldata(1:4,2),
trans_alldata(1:4,3),trans_alldata(1:4,4),trans_alldata(1:4,5));
COMMAND WINDOW RESULT:
YEAR MONTH DAY HOUR MINUTE
2020 3 21 12 2020
3 21 12 2020 3
21 12 2020 3 21
12 2020 3 21 12
*the data is all messed up
The output i'd like to get:
YEAR MONTH DAY HOUR MINUTE
2020 3 21 12 0
2020 3 21 12 10
2020 3 21 12 20
2020 3 21 12 30
I'd appretiate the help.

回答 (1 件)

Star Strider
Star Strider 2022 年 10 月 9 日
data_info = [2020 3 21 12 0
2020 3 21 12 10
2020 3 21 12 20
2020 3 21 12 30
2020 3 21 12 40
2020 3 21 12 50];
trans_alldata = data_info';
fprintf('%d\t%d\t%d\t%d\t%d\n', trans_alldata) % Transpose The Entire Matrix
2020 3 21 12 0 2020 3 21 12 10 2020 3 21 12 20 2020 3 21 12 30 2020 3 21 12 40 2020 3 21 12 50
.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by