![]() |
如何使用MATLAB的num2str格式化输出
我试图在MATLAB中将数字数组输出为字符串。我知道使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.html"]num2str[/URL]可以很容易地做到这一点,但是我想用逗号后跟一个空格来分隔数字,而不是制表符。数组元素最多具有十分之一的分辨率,但其中大多数将是整数。有没有一种方法可以格式化输出,以便避免不必要的尾随零?这是我设法汇总的内容:
data=[2,3,5.5,4]; datastring=num2str(data,'%.1f, '); datastring=['[',datastring(1:end-1),']'] 它给出了输出: [2.0, 3.0, 5.5, 4.0] 而不是: [2, 3, 5.5, 4] 有什么建议么? [B]编辑:[/B]我刚刚意识到我可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strrep.html"]strrep[/URL]通过调用来解决此问题 datastring=strrep(datastring,'.0','') 但这似乎比我一直在做的还要笨拙。 [B]回答:[/B] [B]代替: [/B] datastring=num2str(data,'%.1f, '); [B]尝试: [/B] datastring=num2str(data,'%g, '); [B]输出: [2, 3, 5.5, 4] [/B] [B]要么: [/B] datastring=sprintf('%g,',data); [B]输出: [2,3,5.5,4] [/B] [url=https://stackoverflow.com/questions/3057618]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:04。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.