折叠单元数组到文本matlab

折叠单元数组到文本matlab,matlab,Matlab,我想这是一个基本问题(但我对Matlab不太熟悉),但给出了: >> class(motifIndexAfterThresholds) ans = 'double' 与: 16 8037 14340 21091 27903 34082 作为该变量的内容 我希望在matlab控制台上的同一行打印该变量的内容和一些其他输出: fprintf('With threshold set to %d, %d motifs found at positions %f.\n',thres

我想这是一个基本问题(但我对Matlab不太熟悉),但给出了:

>> class(motifIndexAfterThresholds)
ans =
    'double'
与:

16
8037
14340
21091
27903
34082
作为该变量的内容

我希望在matlab控制台上的同一行打印该变量的内容和一些其他输出:

fprintf('With threshold set to %d, %d motifs found at positions %f.\n',threshold,length(motifIndexAfterThresholds), motifIndexAfterThresholds);
然而,当我这样做时,我得到的输出不止一行:

With threshold set to 800, 6 motifs found at positions 16.000000.
With threshold set to 8037, 14340 motifs found at positions 21091.000000.
With threshold set to 27903, 34082 motifs found at positions

有人能分享一下将这个双数组折叠成一行文本的方法吗?我可以在Matlab控制台上显示它吗?

你需要的是Matlab内置的
num2str
函数。按如下方式修改您的代码:

strThresholds = num2str(motifIndexAfterThresholds.', '%f, '); % Transpose used here since you need to make sure that motifIndexAfterThresholds is a row vector
fprintf('With threshold set to %d, %d motifs found at positions %s.\n',threshold,length(motifIndexAfterThresholds), strThresholds);
num2str
函数将矢量转换为指定格式的字符串。因此,对于您给出的示例

strThresholds = '16.000000,  8037.000000, 14340.000000, 21091.000000, 27903.000000, 34082.000000,'

您完全可以根据需要编辑
num2str
函数中使用的格式字符串。我建议使用
%d
,因为向量中有整数

非常感谢anyanwu!就是这样——但最后一个问题是,您在Motif IndexAfterThresholds变量的末尾添加了一个点和引号,这在语法上有什么作用?(如果我不考虑,结果似乎完全不同)。(这是你提到的转置吗?)这是转置矩阵的方法。请看我在那一行写的评论。您需要确保
motiveIndexAfterThresholds
变量是行向量。因为在你的例子中它是一个列向量,所以我对它进行了转置