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

4 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2015-2-16
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 个评论
Nghiem Le
Nghiem Le 2015-2-16
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 2015-2-16
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by