%% Export Cell Array to Text File
% 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.
% Create a sample cell array C.

C = {'Atkins',32,77.3,'M';'Cheng',30,99.8,'F';'Lam',31,80.2,'M'}

% Export the cell array using writecell.

writecell(C,'data.dat')

% View the contents of the file.

type data.dat

% Alternatively, import the cell array using fprintf. Open a file that you can write to named celldata.dat. Define formatSpec using the format specifiers to describe the pattern of the data in the file. Typical format specifiers include '%s' for a character vector, '%d' for an integer, or '%f' for a floating-point number. Separate each format specifier with a space to indicate a space delimiter for the output file. Include a newline character at the end of each row of data ('\n').

fileID = fopen('celldata.dat','w');
formatSpec = '%s %d %2.1f %s\n';

% Determine the size of C and export one row of data at a time using the fprintf function. Then close the file. fprintf writes a space-delimited file.

[nrows,ncols] = size(C);
for row = 1:nrows
    fprintf(fileID,formatSpec,C{row,:});
end
fclose(fileID);

% View the contents of the file.

type celldata.dat

% Copyright 2012 The MathWorks, Inc.