How can I use fprintf function to display an array sizes of mx n with headers both on columns and rows?

4 views (last 30 days)
Hi,
I'm just wondering how can I use fprintf function to display a table size m x n with headers on both rows and columns. Recently, I had a analyzing data homework where I imported data from a xlsx file and used data to get the mean, median, min, max, and std dev. I was required to have all the statistic in a single matrix but did not required to display headers. I know that I can display header for columns. Below is codes of my other homework.
Table = [x;k]';
fprintf('T(K) \t k(1/min) \n');
fprintf('%3.0f \t %1.2e \n',Table');
It would display " T(K)' and " k(1/min)" as headers for first and second column and data under them. If I would write codes for my data analyzing data problem, I can write the same codes display mean, median, min, max ,and std dev as columns headers.But how can I display headers for the rows.
Thanks, Le

Accepted Answer

dpb
dpb on 16 Feb 2015
Simply incorporate another string field in the format string for the observation name (and remember to include the additional space or other name for the column in the row header for alignment).
When it's a mixed array of character and numeric data, however, you have to use a loop to handle the character array element and the numeric array row elements for each row to intermix the two; if you write the two arrays as the arguments to fprintf separately, it expands each completely in order, not by index within. There's a real advantage of the "implied do" construct in Fortran for i/o
There's a new table tool in last releases as well that will automate this I believe but I don't have newer than R2012b to test....
  2 Comments
Nghiem Le
Nghiem Le on 16 Feb 2015
Thank you for your prompt suggestion ! I'll try to incorporate an additional string for row header and check 'implied do" out as well.
dpb
dpb on 16 Feb 2015
Matlab has no "implied do", I was referring to the construct in Fortran to reference array elements in sequence for ordered i/o; probably shouldn't have brought it into the discussion.
It's not needed so much in Matlab as you can use the ':' operator to refer to the all the elements of the row in the outer loop as
for i=1:nRows
fprintf(fid,fmtString,colHdrStr{i},data(i,:))
end
to write the elements row-by-row.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!