English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Data export (or output) in MATLAB means writing to a file. MATLAB allows you to use your data in another application that reads ASCII files. To do this, MATLAB provides several data export options.
You can create the following types of files-
Rectangles in arrays, separated ASCII data files.
Keypress diary (or log) files and result text output.
Special ASCII files using low-level functions (such as fprintf).
MEX file, used to access C programs written in a specific text file format. / C ++Or Fortran routines.
In addition, you can also export data to spreadsheets.
There are two methods to export numeric arrays as bounded ASCII data files-
UsesaveFunction and specify-asciiQualifier
UsedlmwriteFunction
The syntax of the save function is-
save my_data.out num_array -ascii
where,my_data.outIs the bounded ASCII data file created,num_arrayIs a numeric array, and −asciiIs the specifier.
UsedlmwriteThe syntax of the function is-
dlmwrite('my_data.out', num_array, 'dlm_char')
where,my_data.outIs the bounded ASCII data file created,num_arrayIs a numeric array, dlm_charIs the delimiter.
The following example demonstrates the concept. Create a script file and enter the following code-
num_array = [ 1 2 3 4 ; 4 5 6 7; 7 8 9 0]; save array_data1.out num_array -ascii; type array_data1.out dlmwrite('array_data2.out', num_array, ' type array_data2.out
When the file is executed, it displays the following result-
1.0000000e+00 2.0000000e+00 3.0000000e+00 4.0000000e+00 4.0000000e+00 5.0000000e+00 6.0000000e+00 7.0000000e+00 7.0000000e+00 8.0000000e+00 9.0000000e+00 0.0000000e+00 1 2 3 4 4 5 6 7 7 8 9 0
Note that save -The ascii command and dlmwrite function are not applicable to cell arrays used as input. To create a bounded ASCII file from the content of a cell array, you can
Usecell2matThe function converts cell arrays to matrices
Or use the low-level file I / O function to export unit arrays.
If usingsaveIf the function writes a character array to an ASCII file, it is equivalent to writing the ASCII of the character to the file.
For example, let's write the word 'hello' to a file-
h = 'hello'; savetextdata.outh -ascii type textdata.out
MATLAB executes the above statements and displays the following results. This is8The character of the ASCII format string 'hello'.
1.0400000e+02 1.0100000e+02 1.0800000e+02 1.0800000e+02 1.1100000e+02
The diary file is the activity log of your MATLAB session. The diary feature can create an exact copy of the session in a disk file, but not including graphics.
To open the diary function, please enter-
diary
(Optional) You can provide the name of the log file, for example-
diary logdata.out
Close the diary function-
diary off
You can open the diary file in a text editor.
So far, we have exported numeric arrays. However, you may need to create other text files, including combinations of numeric and character data, non-rectangular output files, or files with non-ASCII encoding schemes. For this purpose, MATLAB provides the lower levelfprintffunction.
with the lower level I / Just like in file activities at the lower level, before exporting, you need to usefopenfunction to open or create a file and get the file identifier. By default, fopen opens the file for read-only access. You should specify write or append permissions, such as 'w' or 'a'.
After processing the file, you need to usefclose(fid)The function closes it.
The following examples demonstrate the concept-
Create a script file and enter the following code-
%Create a matrix y with two rows x = 0:10:100; y = [x; log(x)]; %Open the file for writing fid = fopen('logtable.txt', 'w'); %Table title fprintf(fid, 'Log Function\n\n'); %Values are printed in column order %Two values are displayed on each line of the file fprintf(fid, '%f %f\n', y); fclose(fid); %Display the created file type logtable.txt
When the file is executed, it displays the following result-
Log Function 0.000000 -Inf 10.000000 2.302585 20.000000 2.995732 30.000000 3.401197 40.000000 3.688879 50.000000 3.912023 60.000000 4.094345 70.000000 4.248495 80.000000 4.382027 90.000000 4.499810 100.000000 4.605170