In recent projects, you need to use MATLAB's excellent matrix computing capabilities, and then save the matrix to a file for use by programs in other languages. This requires that the matrix be saved as a file format, not a mat form. Here are the main uses of three methods: by Muzi Xiao Son
1. Dlmwrite: Write ASCII-encoded delimited matrix.
function dlmwrite (filename, M, varargin)%dlmwrite Write ASCII delimited file.%% dlmwrite (' filename ', m) writes Matrix M i nto filename using ', ' as the% delimiter to separate matrix elements.%% dlmwrite (' FILENAME ', M, ' DLM ') writes matrix M i Nto FILENAME using the% character DLM as the delimiter.%% dlmwrite (' FILENAME ', M, ' DLM ', r,c) writes matrix M starting at % offset row R, and offset column C in the file. R and C is zero-based,% so this r=c=0 specifies the first value in the file.%% dlmwrite (' FILENAME ', M, ' ATTRIBUTE1 ', ' VA LUE1 ', ' ATTRIBUTE2 ', ' VALUE2 ' ...) % an alternative calling syntax this uses attribute value pairs for% specifying optional arguments to Dlmwrite. The order of the% Attribute-value pairs does not matter, as long as a appropriate value% follows each attribute tag. %%dlmwrite (' FILENAME ', M, '-append ') appends the matrix to the File.%without the flag, Dlmwrite overwrites any existing fil E.%%dlmwrite (' FILENAME ', M, '-append ', ' ATTRIBUTE1 ', ' VALUE1 ',...) %is THe same as the previous syntax, but accepts attribute value Pairs,%as well as the '-append ' flag. The flag can be placed in the argument%list anywhere between attribute value pairs, but not between An%attribute and its V alue.%% USER Configurable options%% attribute:a quoted string defining an ATTRIBUTE tag. The following% attribute tags are valid-% ' delimiter ' = delimiter string to be used in Separati ng matrix% elements.% ' newline ' = ' pc ' use CR/LF as line terminator% ' UNIX ' use LF as Line terminator% ' roffset ' = zero-based offset, in rows, from the top of the% Destination file to where the data it to be% written. % ' coffset ' = zero-based offset, in columns, from the left side% of the destination fil E to where the data are to be% written.% ' precision ' = Numeric precision to use with writing data to the% file, as significant digits or a c-style format% String, starting with '% ', such as '%10.5f '. Note% the uses the operating system standard% library to truncate the NU mber.%%% examples:%% dlmwrite (' Abc.dat ', M, ' delimiter ', '; ', ' Roffset ', 5, ' Coffset ', 6,...% ' precision ', 4) writes matrix M to row offset 5, column offset 6, in% file abc.dat using; As the delimiter between matrix elements. the% Numeric precision is of the data are set to 4 significant decimal% digits.%% dlmwrite (' Example.dat ', M, '-append ') Appends matrix M to the end of% the file Example.dat. By default append mode is off, i.e dlmwrite% overwrites the existing file.%% dlmwrite (' Data.dat ', M, ' delimiter ', ' \ t ', ' Precision ', 6) writes M to file% ' data.dat ' with elements delimited by the tab character, using a precision% of 6 signi Ficant digits.%% dlmwrite (' file '.TXT ', M, ' delimiter ', ' \ t ', ' precision ', '%.6f ') writes M% to file file.txt with elements delimited by the tab character, USI Ng a% precision of 6 decimal places. Percent Dlmwrite (' Example2.dat ', M, ' newline ', ' pc ') writes M to file% Example2.dat, using the conventional line terminator fo R the pc% platform.%% see also Dlmread, Csvwrite, Num2str, sprintf.% Brian M. bourgault 10/22/93% modified:jp Bar Nard, September 2002.% Michael Theriault, 6 November 2003% Copyright 1984-2011 the MathWorks, inc.%----- --------------------------------------------------------------------------
Given matrix M, the matrix is written to the file with the reader-specified delimiter, which can be controlled in the form of a C language, such as '%u ' for shaping, and '%5.6f ' represents a floating-point type of 5-bit integer 6-bit decimal.
This method is formatted by the row of the Matrix, and a line of writing to the file, the efficiency is slow, I tested the row 10^7, column 2 of the Matrix, with 880s.
2.num2str, and then fprintf, converts the matrix to a string, and then writes the matrix in Fprinf.
%num2str Convert numbers to a string.% T = Num2str (X) converts the matrix X into a string representation t% with about 4 digits and an exponent if required. This was useful for% labeling plots with the TITLE, Xlabel, Ylabel, and TEXT commands.%% T = num2str (x,n) converts the Matrix X into a string representation% with a maximum N digits of precision. The default number of digits is% based on the magnitude of the elements of x.%% T = Num2str (X,format) uses the FORMAT String FORMAT (see SPRINTF for% details). Percent Example 1:% num2str (Randn (2,2), 3) produces a string matrix such as%% 1.44-0.755% 0.325 1.37%% Example 2:% num2str (rand (2,3) * 9999, '%10.5e\n ') produces a strin G matrix% such as%% 8.14642e+03% 1.26974e+03% 6.32296e+03% 9.05701e+03% 9.13285e+03% 9.75307e+02%% See also Int2str, SPRINTF, fprintf, mat2str.% Copyright 1984-2012 the MathWorks, inc.%------------- -----------------------------------------------------------------
Num2str transforms a matrix into a string, which is almost as efficient and int2str, much faster than MAT2STR. Num2str can also be controlled in the form of a C language to convert a number into a string. Conversions are listed as units. Efficient, conversion line 10^7, column 2 of the matrix less than 1s. The generated memory is about twice times that of the original matrix, but the intermediate process takes up a large amount of memory, and the large matrix can be considered for chunking. Then you write the characters in fprintf to the file.
3.fprintf function
%FPRINTF Write formatted data to text file.% fprintf (FID, FORMAT, A, ...) applies the FORMAT to all elements of% ARRA Y A and any additional arrays arguments in column order, and writes% the data to A text file. FID is an integer file identifier. Obtain% FID from FOPEN, or set it to 1 (for standard output, the screen) or 2% (standard error). FPRINTF uses the encoding scheme specified in the% call to fopen.%% fprintf (FORMAT, A, ...) formats data and displays The results on the% screen.%% COUNT = fprintf (...) returns the number of bytes that fprintf writes.%% FORMAT is a St Ring that describes the format of the output fields, and% can include combinations of the following:%% * Conversion Specifications, which include a% character, a% conversion character (such as D, I, O, u, x, F, E, G, C, or s),% and optional flags, width, and precision fields. For more% details, type ' Doc fprintf ' at the command prompt.%% * Literal text to print.%% * Escape characters, including:% \b Backspace "single quotation mark% \f Form feed Percent character% \ New line \ \ backslash% \ r C Arriage return \xn hexadecimal number N% \ t horizontal tab \ n Octal number N% for most Cases, \ n is sufficient for a single line break.% However, if you be creating a file for use with microsoft% Notepad, specify a combination of \ r \ n to move to a new line.%% notes:%% If you apply an integer or string conversi On to a numeric value that% contains a fraction, MATLAB overrides the specified conversion, and% uses%e.%% numeric Conversions print only the real component of complex numbers.%% example:create a text file called Exp.txt containing a Short table of% the exponential function.%% x = 0:.1:1;% y = [x; exp (x)];% fid = fopen (' Exp.txt ', ' W '); % fprintf (FID, '%6.2f%12.8f\n ', y);% fclose (FID); percent examine the contents of exp.txt:%% type exp.txt%% MATLAB returns:% 0.00 1.00000000% 0.10 1.10517092% ...% 1.00 2.71828183%% See also FOPEN, FC Lose, FSCANF, FREAD, FWRITE, SPRINTF, disp.% Copyright 1984-2009 the MathWorks, inc.% built-in function.
This function converts the matrix to a file in the specified format after conversion, with the advantage of being small in memory, (writing on edge conversion), high efficiency (as in the second method time), and recommending this method.
Comparison of several methods of saving matrix in matlab