Matlab打印垫 [英] Matlab printmat
问题描述
我是Matlab的新手.有没有办法使用printmat
打印2个单词的标题?
I am new to Matlab. Is there a way to use printmat
to print 2 words heading?
示例结果如下:
Title One Title Two Title Three
11 22 33
22 33 44
这是我当前尝试修改的代码:
Here is the code i currently trying to modify:
matA = [ 11 22 33; 22 33 44];
printmat(matA, '' , '' , 'TitleOne TitleTwo TitleThree');
我似乎无法在标题"和一个"之间添加空格,其中添加空格始终会导致以下结果:
I can't seems to add a space in between 'Title' and 'One' where adding the space always result in the following outcome:
printmat(matA, '' , '' , 'Title One Title Two Title Three');
Title One Title
11 22 33
22 33 44
感谢您的帮助.
推荐答案
根据Matlabs的帮助,printmat
将不会提供您要查找的内容.您可以改用sprintf
.
According to Matlabs help, printmat
will not provide what you are seeking. You can use sprintf
instead.
a = [ 11 22 33; 22 33 44];
s = {'Title One' 'Title Two' 'Title Three'};
s1 = sprintf('%12s\t%12s\t%12s\t\n', s{:});
s2 = sprintf('%12d\t%12d\t%12d\n', a);
horzcat(s1,s2)
这导致
ans =
Title One Title Two Title Three
11 22 22
33 33 44
〜编辑〜
如果最好使用printmat
(例如,因为它更灵活),则可以使用evalc
和strrep
解决.这里的技巧是在调用printmat
时用其他符号(例如问号)替换空格,通过evalc
将输出存储在字符串中,然后使用strrep
用空格替换问号.作为一个很好的副产品,您可以将表作为字符串...
~edit~
If the use of printmat
is preferable (e.g., because it is more flexible) you can work around by using evalc
and strrep
. The trick here is to replace the spaces with other symbols (e.g. question marks) in the call to printmat
, store the output in a string via evalc
, and then use strrep
to replace the question marks by spaces. As a nice byproduct, you get the table as a string...
a = [ 11 22 33; 22 33 44];
x = evalc('printmat(matA, '''' , ''a b c'' , ''Title?One Title?Two Title?Three'')');
s = strrep(x, '?', ' ')
这将导致
s =
Title One Title Two Title Three
a 11.00000 22.00000 33.00000
b 22.00000 33.00000 44.00000
但是,printmat和evalc的组合会引起很多撇号...
However, the combination of printmat and evalc causes a lot of apostrophes...
这篇关于Matlab打印垫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!