MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

6 Data Import and Export A = [ 1 2 3 4 ; 5 6 7 8 ]; use the save function, as follows: save my_data.out A -ASCII If you view the created file in a text editor, it looks like this: 1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000 5.0000000e+000 6.0000000e+000 7.0000000e+000 8.0000000e+000 By default, save uses spaces as delimiters but you can use tabs instead of spaces by specifying the -tabs option. When you use save to write a character array to an ASCII file, it writes the ASCII equivalent of the characters to the file. If you write the character string 'hello' to a file, save writes the values 104 101 108 108 111 Using the dlmwrite Function To export an array in ASCII format and specify the delimiter used in the file, use the dlmwrite function. For example, to export the array A, A = [ 1 2 3 4 ; 5 6 7 8 ]; as an ASCII data file that uses semicolons as a delimiter, use this command: dlmwrite('my_data.out',A, ';') If you view the created file in a text editor, it looks like this: 1;2;3;4 5;6;7;8 Note that dlmwrite does not insert delimiters at the end of rows. 6-86

Exporting Text Data By default, if you do not specify a delimiter, dlmwrite uses a comma as a delimiter. You can specify a space (' ') as a delimiter or, if you specify empty quotes (''), no delimiter. Using the diary Function to Export Data To export small numeric arrays or cell arrays, you can use the diary function. diary creates a verbatim copy of your MATLAB session in a disk file (excluding graphics). For example, if you have the array A in your workspace, A = [ 1 2 3 4; 5 6 7 8 ]; execute these commands at the MATLAB prompt to export this array using diary: 1 Turn on the diary function. You can optionally name the output file diary creates. diary my_data.out 2 Display the contents of the array you want to export. This example displays the array A. You could also display a cell array or other MATLAB data type. A = 1 2 3 4 5 6 7 8 3 Turn off the diary function. diary off diary creates the file my_data.out and records all the commands executed in the MATLAB session until it is turned off. A = 1 2 3 4 5 6 7 8 6-87

6 Data Import and Export<br />

A = [ 1 2 3 4 ; 5 6 7 8 ];<br />

use the save function, as follows:<br />

save my_data.out A -ASCII<br />

If you view the created file in a text editor, it looks like this:<br />

1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000<br />

5.0000000e+000 6.0000000e+000 7.0000000e+000 8.0000000e+000<br />

By default, save uses spaces as delimiters but you can use tabs instead of<br />

spaces by specifying the -tabs option.<br />

When you use save to write a character array to an ASCII file, it writes the<br />

ASCII equivalent of the characters to the file. If you write the character string<br />

'hello' to a file, save writes the values<br />

104 101 108 108 111<br />

Using the dlmwrite Function<br />

To export an array in ASCII format and specify the delimiter used in the file,<br />

use the dlmwrite function.<br />

For example, to export the array A,<br />

A = [ 1 2 3 4 ; 5 6 7 8 ];<br />

as an ASCII data file that uses semicolons as a delimiter, use this command:<br />

dlmwrite('my_data.out',A, ';')<br />

If you view the created file in a text editor, it looks like this:<br />

1;2;3;4<br />

5;6;7;8<br />

Note that dlmwrite does not insert delimiters at the end of rows.<br />

6-86

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!