string 如何将字符串和矩阵写入 MATLAB 中的 .txt 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4556971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I write strings and matrices to a .txt file in MATLAB?
提问by Maddy
I need to write data to a .txt file in MATLAB. I know how to write strings (fprintf
) ormatrices (dlmwrite
), but I need something that can do both of them. I'll give an example below:
我需要将数据写入 MATLAB 中的 .txt 文件。我知道如何写字符串 ( fprintf
)或矩阵 ( dlmwrite
),但我需要一些可以同时完成它们的东西。下面我举个例子:
str = 'This is the matrix: ' ;
mat1 = [23 46 ; 56 67] ;
%fName
if *fid is valid*
fprintf(fid, '%s\n', str)
fclose(fid)
end
dlmwrite(fName, *emptymatrix*, '-append', 'delimiter', '\t', 'newline','pc')
dlmwrite(fName, mat1, '-append', 'newline', 'pc')
This works okay, but with a problem. The first line of the file is:
这工作正常,但有问题。文件的第一行是:
This is the matrix: 23,46
Which is not what I want. I want to see:
这不是我想要的。我想看看:
This is the matrix:
23 46
56 67
How can I solve this? I can't use a for loop and printf
solution as the data is huge and time is an issue.
我该如何解决这个问题?我不能使用 for 循环和printf
解决方案,因为数据很大而且时间是一个问题。
回答by gnovice
I think all you have to do to fix your problem is add a carriage return (\r
) to your FPRINTFstatement and remove the first call to DLMWRITE:
我认为解决问题所需要做的就是\r
在FPRINTF语句中添加一个回车 ( )并删除对DLMWRITE的第一次调用:
str = 'This is the matrix: '; %# A string
mat1 = [23 46; 56 67]; %# A 2-by-2 matrix
fName = 'str_and_mat.txt'; %# A file name
fid = fopen(fName,'w'); %# Open the file
if fid ~= -1
fprintf(fid,'%s\r\n',str); %# Print the string
fclose(fid); %# Close the file
end
dlmwrite(fName,mat1,'-append',... %# Print the matrix
'delimiter','\t',...
'newline','pc');
And the output in the file looks like this (with tabs between the numbers):
文件中的输出如下所示(数字之间有制表符):
This is the matrix:
23 46
56 67
NOTE:A short explanation... the reason for needing the \r
in the FPRINTFstatement is because a PC line terminator is comprised of a carriage return followed by a line feed, which is what is used by DLMWRITEwhen the 'newline','pc'
option is specified. The \r
is needed to ensure the first line of the matrix appears on a new line when opening the output text file in Notepad.
注意:简短的解释...\r
在FPRINTF语句中需要 的原因是因为 PC 行终止符由回车符和换行符组成,这是指定选项时DLMWRITE使用的'newline','pc'
。的\r
需要,以确保基质出现的第一行上的新线在记事本中打开该输出文本文件时。
回答by Charles L.
You don't need the empty matrix call. Try this code:
您不需要空矩阵调用。试试这个代码:
str = 'This is the matrix: ' ;
mat1 = [23 46 ; 56 67] ;
fName = 'output.txt';
fid = fopen('output.txt','w');
if fid>=0
fprintf(fid, '%s\n', str)
fclose(fid)
end
dlmwrite(fName, mat1, '-append', 'newline', 'pc', 'delimiter','\t');
回答by Andrew Janke
You've got two dlmwrite() calls, the first on an empty matrix, and the second one is missing the 'delimiter' option. What happens if you add it to the second call?
您有两个 dlmwrite() 调用,第一个在空矩阵上,第二个缺少 'delimiter' 选项。如果将它添加到第二个调用中会发生什么?
回答by mlanzero
I ran into a similar situation adding a header to a csv. You can use dlmwrite with -append to add a single line by setting your delimiter equal to ''
as shown below.
我遇到了类似的情况,将标题添加到 csv。您可以使用带有 -append 的 dlmwrite 来添加一行,方法是将分隔符设置''
为等于,如下所示。
str = 'This is the matrix: '; %# A string
mat1 = [23 46; 56 67]; %# A 2-by-2 matrix
fName = 'str_and_mat.txt'; %# A file name
header1 = 'A, B'
dlmwrite(fName, str, 'delimiter', '')
dlmwrite(fName, header1, '-append', 'delimiter', '')
dlmwrite(fName, mat1, '-append','delimiter', ',')
This produces the following:
这会产生以下结果:
This is the matrix:
A, B
23,46
56,67