如何在 MATLAB 中构建 "excel-friendly" 字符串?
How to construct an "excel-friendly" string in MATLAB?
我想构造一个table如下:
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
(|
字符只代表一个新列,字符串中不需要)
我只需要构建 "inner" 部分,即仅将数字作为字符串的部分并将其复制到剪贴板,这样我就可以转到 Excel 并将整个内容粘贴到一次。这可能吗?如果是这样,我将不胜感激。
到目前为止我尝试过的:
我尝试按如下方式构造字符串:
str = [num2str(fscore1,2) ' (' num2str(fscore2,2) ')\t etc'];
显然 '\t'
不符合我的目的。我也不知道如何自动将字符串复制到剪贴板。因此,我们将不胜感激。
您尝试执行的操作的主要问题是简单的字符串连接(使用 []
或 strcat
)将 \t
视为字符串文字(字符 \
后跟字符 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')
% a b
% Or using char(9)
['a', char(9), 'b']
% a b
对于 "Excel-friendly" 字符串,您希望在行中的值之间使用一些分隔符(可能最简单的制表符),然后在行之间使用换行符。我们可以使用 sprintf
轻松构建这样的字符串(请参见下面的代码片段)。
就自动将内容复制到剪贴板而言,内置的 clipboard
函数允许您将字符串复制到系统剪贴板。您可以从您的数据构建一个制表符分隔的字符串并将其存储在剪贴板中。然后你可以将它粘贴到 Excel(或任何程序)。
您需要构建与此类似的字符串:
% 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(或相关程序)中以生成如下内容:
另一种选择是将数据放入元胞数组中,您可以使用工作区变量编辑器将其可视化。从工作区查看器中,您可以复制内容(就像在 Excel 中一样)并将它们粘贴到任何程序中。
我想构造一个table如下:
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
(|
字符只代表一个新列,字符串中不需要)
我只需要构建 "inner" 部分,即仅将数字作为字符串的部分并将其复制到剪贴板,这样我就可以转到 Excel 并将整个内容粘贴到一次。这可能吗?如果是这样,我将不胜感激。
到目前为止我尝试过的:
我尝试按如下方式构造字符串:
str = [num2str(fscore1,2) ' (' num2str(fscore2,2) ')\t etc'];
显然 '\t'
不符合我的目的。我也不知道如何自动将字符串复制到剪贴板。因此,我们将不胜感激。
您尝试执行的操作的主要问题是简单的字符串连接(使用 []
或 strcat
)将 \t
视为字符串文字(字符 \
后跟字符 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')
% a b
% Or using char(9)
['a', char(9), 'b']
% a b
对于 "Excel-friendly" 字符串,您希望在行中的值之间使用一些分隔符(可能最简单的制表符),然后在行之间使用换行符。我们可以使用 sprintf
轻松构建这样的字符串(请参见下面的代码片段)。
就自动将内容复制到剪贴板而言,内置的 clipboard
函数允许您将字符串复制到系统剪贴板。您可以从您的数据构建一个制表符分隔的字符串并将其存储在剪贴板中。然后你可以将它粘贴到 Excel(或任何程序)。
您需要构建与此类似的字符串:
% 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(或相关程序)中以生成如下内容:
另一种选择是将数据放入元胞数组中,您可以使用工作区变量编辑器将其可视化。从工作区查看器中,您可以复制内容(就像在 Excel 中一样)并将它们粘贴到任何程序中。