printing matrix data in the correct order (fprintf)

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by