printing matrix data in the correct order (fprintf)
4 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Star Strider
on 9 Oct 2022
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
.
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!