Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何构建一个;“卓越友好”;MATLAB中的字符串?_String_Excel_Matlab_Clipboard - Fatal编程技术网

String 如何构建一个;“卓越友好”;MATLAB中的字符串?

String 如何构建一个;“卓越友好”;MATLAB中的字符串?,string,excel,matlab,clipboard,String,Excel,Matlab,Clipboard,我想按如下方式构造一个表: 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 (字符|仅表示一个新列,而不是字符串中所必需的) 我只需要构建“内部”部分,即以数字作为字符串的部分,并将其复制到剪贴板,这样我就可

我想按如下方式构造一个表:

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
(字符
|
仅表示一个新列,而不是字符串中所必需的)

我只需要构建“内部”部分,即以数字作为字符串的部分,并将其复制到剪贴板,这样我就可以立即转到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友好”字符串,您希望在行中的值之间使用一些分隔符(可能最简单的是制表符),然后在行之间使用换行符。我们可以使用
sprintf
(参见下面的代码片段)轻松地构造这样一个字符串

就自动将内容复制到剪贴板而言,内置函数允许您将字符串复制到系统剪贴板。您可以从数据构造以制表符分隔的字符串,并将其存储在剪贴板中。然后您可以将其粘贴到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中一样)并将其粘贴到任何程序中。

感谢您的全面回答。