添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在Matlab中用逗号分隔字符串

0 人关注

我想在Matlab中用逗号来分隔文本文件中的字符串,到目前为止,我已经使用了 "csvwrite "和 "dlmwrite"。

它们一般采取这种形式。

myFile - input ('Please enter file's directory','s');
readMatrix - csvread(myFile);
csvwrite('newdatafile.dat', readMatrix);

澄清一下,我想做的事情是这样的。

转一个文本文件,看起来像这样。

为了这个。

0.3,
0.1,
0.5,
2 个评论
+1 发布输入和输出的样本。但更重要的是,你应该包括一点关于到底是什么东西不工作。另外,在这种情况下,你可以给出一些关于你如何输入文件和存储文件的更多细节。
读取原始文件后,readMatrix的格式是什么?它是一个矩阵(如果是的话,有多大?),还是一个单元格阵列?
matlab
csv
comma
data-processing
Tony Hematite
Tony Hematite
发布于 2012-05-13
2 个回答
Gunther Struyf
Gunther Struyf
发布于 2012-05-13
已采纳
0 人赞同

为什么你需要在行尾加上逗号? csvread 也适用于行间包含逗号分隔的文件,即。

file1.dat:
02, 04, 06, 08, 10, 12
03, 06, 09, 12, 15, 18
05, 10, 15, 20, 25, 30
07, 14, 21, 28, 35, 42
11, 22, 33, 44, 55, 66
--and then in matlab:
readMatrix = csvread('file1.dat')

将读取文件,结果是size(readMatrix) = [5 6](尽管每行末尾缺少逗号) 如果文件只包含一列数字(因此没有逗号),同样的情况也适用。

然而,如果你真的想要这些逗号,你可以自己用fprintf读取数据并打印到一个文件中。

readMatrix = csvread('file1.dat');
fid=fopen('file1withcommas.dat','w');
for ii=1:size(readMatrix,1)
    fprintf(fid,'%g,\n',readMatrix(ii,:));