Skip to main content

9.5) Writing Text Files


The simplest way of saving array data to text files is to use the "save" command. To save data, we require a definition of the output format. For example:

[matlab]
>> A=randn(10,2) % Generate some normally distributed random numbers
>> save('random_data.txt', 'A', '-ascii') % Save as a text file
[/matlab]

Matlab has many high level functions to read and write ascii and binary data. The main functions are:

fopen Opens a file for reading and/or writing
fscanf Read an ascii file
fprintf Write an ascii file
fread Read a binary file
fwrite Write a binary file
fclose Close a file