如何在MATLAB中构build“excel友好”的string?
我想要构build一个表格如下:
Feature | F-score | Precision | Recall | Feature1 | 0.81 (0.82) | 0.83 (0.84) | 0.85 (0.86) | Feature2 | 0.87 (0.88) | 0.83 (0.84) | 0.85 (0.86) | .. etc
( |
字符只是表示一个新的列,不需要在string中)
我只需要构build“内部”部分,即将数字作为string的部分,并将其复制到剪贴板,以便我可以去Excel并一次粘贴整个事情。 这可能吗? 如果是这样,我将不胜感激一个工作的例子。
我到目前为止所尝试的:
我试图构造如下的string:
str = [num2str(fscore1,2) ' (' num2str(fscore2,2) ')\t etc'];
显然'\t'
不适合我的目的。 我也不知道如何将string自动复制到剪贴板。 所以任何帮助,将不胜感激。
你要做什么的主要问题是简单的string连接(使用[]
或strcat
)将\t
作为string文字(字符\
后跟字符t
)而不是控制序列。 相反,你会想要使用sprintf
或fprintf
或char(9)
(9是ASCII的标签)有制表符。
% The way that you tried ['a\t', 'b']; % 'a\tb' % The way that it should be sprintf('a\tb') % ab % Or using char(9) ['a', char(9), 'b'] % ab
对于一个“Excel友好”的string,你想在你的行之间使用一些分隔符(可能是最简单的标签),然后在行之间换行符。 我们可以使用sprintf
轻松地构build这样的string(请参阅下面的代码片段)。
至于自动复制到剪贴板的东西,内置的clipboard
function允许您将string复制到系统剪贴板。 您可以从数据中构造制表符分隔的string,并将其存储在剪贴板中。 然后,您可以将其粘贴到Excel(或任何程序)。
您需要构build类似于以下的string:
% Labels for the columns header = sprintf('%s\t', 'Feature', 'F-score', 'Precision', 'Recall'); % Store your data within a cell array data = {'Feature1', 0.81, 0.82, 0.83, 0.84, 0.85, 0.86; 'Feature2', 0.87, 0.88, 0.83, 0.84, 0.85, 0.86}.'; % Construct your tab-delimited string with newlines between rows datastring = sprintf('%s\t%0.2f (%0.2f)\t%0.2f (%0.2f)\t%0.2f (%0.2f)\n', data{:}); % Append the header to the rest of the data fullstring = sprintf('%s\n%s', header, datastring); % Copy this string to the system clipboard clipboard('copy', fullstring);
您可以将结果粘贴到Excel(或相关程序)中,以产生如下所示的内容:
另一个select是将数据放入单元格数组中,您可以使用Workspacevariables编辑器将其可视化。 从工作区查看器中,可以复制内容(就像在Excel中一样)并将其粘贴到任何程序中。