How can I write strings and matrices to a .txt file in MATLAB?

情到浓时终转凉″ 提交于 2019-12-04 09:26:42

问题


I need to write data to a .txt file in MATLAB. I know how to write strings (fprintf) or matrices (dlmwrite), but I need something that can do both of them. I'll give an example below:

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.


回答1:


I think all you have to do to fix your problem is add a carriage return (\r) to your FPRINTF statement and remove the first call to 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 FPRINTF statement is because a PC line terminator is comprised of a carriage return followed by a line feed, which is what is used by DLMWRITE when 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.




回答2:


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');



回答3:


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?




回答4:


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.

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


来源:https://stackoverflow.com/questions/4556971/how-can-i-write-strings-and-matrices-to-a-txt-file-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!