I have problem with displaying a matrix with command fprintf

4 次查看(过去 30 天)
Hi Everybody
I have problem with displaying a matrix with command fprintf, a string line is too long and I like to divided in to two parts but I couldn't do it. Is there any one have an idea how to separate it it into parts. I copied the code, the problem is with third line.
Thanks in advance
z=rand(4,15);
fprintf(1,'+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+\n')
fprintf(1,'| %7.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.3f |%8.3f |%10.3f|%11.3f| %9.4f |\n+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+\n',z')

采纳的回答

Walter Roberson
Walter Roberson 2015-8-8
row_divider = '+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+';
row_fmt = '| %7.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.3f |%8.3f |%10.3f|%11.3f| %9.4f |';
z=rand(4,15);
zc = mat2cell(z, ones(1,size(z,1)), size(z,2));
zaug = [zc(:), repmat({row_divider},size(z,1),1)] .';
fprintf(1, '%s\n', row_divider);
fprintf(1, [row_fmt '\n%s\n'], zaug{:});
Or more simply,
row_divider = '+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+';
z=rand(4,15);
fprintf(1,'%s\n', row_divider)
fprintf(1, ['| %7.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.3f |%8.3f |%10.3f|%11.3f| %9.4f |\n' row_divider '\n'],z')
The first version shows how you can mix data strings with rows of values: create a cell with the data and {:} expand the mixed-type cell.

更多回答(1 个)

Image Analyst
Image Analyst 2015-8-8
So just use 4 or 5 or 6 fprintf's. What's the problem? For example:
fprintf(1,'| %7.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.3f |%8.3f |%10.3f|%11.3f| %9.4f |\n',z');
fprintf(1,'+---------+---------+----------+-----------+-----------+---------+---------+')
fprintf(1,'----------+-----------+-----------+---------+---------+----------+-----------+-----------+\n')
  4 个评论
Image Analyst
Image Analyst 2015-8-8
I'm not sure I understand either of you. I took a really really long fprintf() line (the third line of code he had) and split it up into 3 lines so that more of it fit in the viewport of the editor. I thought this is what he wanted when he said the "line is too long and I like to divided in to two parts". He obviously knows that he can put a line feed \n anywhere to put the stuff on a new output line - he's already done that so of course he knows that. He originally did not have a line feed somewhere in the middle of all the +---- so why would I put one there? So his single fprintf()
fprintf(1,'| %7.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.3f |%8.3f |%10.3f|%11.3f| %9.4f |\n+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+\n',z')
and my multiple fprintf()'s
fprintf(1,'| %7.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.4f |%8.3f |%10.2f|%11.2f| %9.4f |%8.3f |%8.3f |%10.3f|%11.3f| %9.4f |\n',z'); fprintf(1,'+---------+---------+----------+-----------+-----------+---------+---------+') fprintf(1,'----------+-----------+-----------+---------+---------+----------+-----------+-----------+\n')
print exactly the same thing. Mine is just not as wide across the page because there is three short lines of code instead of one much longer one. Sometimes it is annoying to have super long lines of code - I even break up lines into multiple lines myself sometimes. It's annoying that you can't continue a string with ... like you can in other places in your code.
But I could be wrong. Maybe when he said "line is too long..." he meant the output line and for some strange reason incorrectly thought he was only allowed to put the \n at the end of the format specifier string. Not sure why anyone would think that though.
Walter Roberson
Walter Roberson 2015-8-8
The poster did not like how long the line of code was becoming and wanted to split it to make shorter lines.
The poster wanted the dividing line between every row of output, like this:
+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+
| 0.1200 | 0.209 | 0.99| 0.01| 0.5993 | 0.5145 | 0.420 | 0.41| 0.53| 0.0914 | 0.481 | 0.069 | 0.639| 0.577| 0.4409 |
+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+
| 0.8863 | 0.077 | 0.48| 0.99| 0.0837 | 0.7872 | 0.265 | 0.92| 0.33| 0.3187 | 0.340 | 0.942 | 0.507| 0.644| 0.1107 |
+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+
| 0.6475 | 0.624 | 0.37| 0.25| 0.4464 | 0.3291 | 0.193 | 0.86| 0.21| 0.2687 | 0.100 | 0.921 | 0.782| 0.946| 0.3923 |
+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+
| 0.0672 | 0.528 | 0.89| 0.35| 0.5484 | 0.2307 | 0.669 | 0.52| 0.96| 0.4268 | 0.745 | 0.176 | 0.691| 0.431| 0.5009 |
+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+---------+---------+----------+-----------+-----------+
Your suggested code would be for presenting the interior lines like
| 0.1200 | 0.209 | 0.99| 0.01| 0.5993 | 0.5145 | 0.420 | 0.41| 0.53| 0.0914 | 0.481 | 0.069 | 0.639| 0.577| 0.4409 |
| 0.8863 | 0.077 | 0.48| 0.99| 0.0837 | 0.7872 | 0.265 | 0.92| 0.33| 0.3187 | 0.340 | 0.942 | 0.507| 0.644| 0.1107 |
| 0.6475 | 0.624 | 0.37| 0.25| 0.4464 | 0.3291 | 0.193 | 0.86| 0.21| 0.2687 | 0.100 | 0.921 | 0.782| 0.946| 0.3923 |
| 0.0672 | 0.528 | 0.89| 0.35| 0.5484 | 0.2307 | 0.669 | 0.52| 0.96| 0.4268 | 0.745 | 0.176 | 0.691| 0.431| 0.5009 |
Recall that when you have more data then format elements then the entire format gets reused, including the constant strings.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by