Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 989
Maddy Avatar asked Dec 29 '10 19:12

Maddy


People also ask

How do you write a matrix to a text file in MATLAB?

Write Matrix to Text File Write the matrix to a comma delimited text file and display the file contents. The writematrix function outputs a text file named M. txt . To write the same matrix to a text file with a different delimiter character, use the 'Delimiter' name-value pair.

How do I export data from MATLAB to text?

You can export a cell array from MATLAB® workspace into a text file in one of these ways: Use the writecell function to export the cell array to a text file. Use fprintf to export the cell array by specifying the format of the output data.

Can you put strings in a matrix MATLAB?

You can access strings in a string array with matrix indexing, just as you would access elements of a numeric array.

How do I convert a string to a matrix in MATLAB?

X = str2num( txt ) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements.


2 Answers

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.

like image 156
gnovice Avatar answered Oct 19 '22 06:10

gnovice


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');
like image 27
Charles L. Avatar answered Oct 19 '22 05:10

Charles L.