%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% A more general purpose (non-vectorized) example of using FPRINTF()
% to output text file data is presented below:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%first, let's create some data
string = 'New York';
floating_point = 99.99;
integer = int16(1024);

%now, let's open/create a text-mode output file (i.e., 'wt')
%   filename = 'textoutput.txt'
%   fid = file ID number returned by MATLAB
%         If FID = -1 then FOPEN() was unsuccessful
%         and an error message is returned
[fid, message] = fopen('textoutput.txt','wt');

%exit if FOPEN() was unsuccessful
if( fid == -1 )
    disp('fopen() failed');
    disp(message);
    return; %exit script
end

%let's write some data to the successfully opened output file
%we'll need to use it's file ID number (i.e., fid) to do so...
fprintf( fid, 'string data follows: %s\n', string );
fprintf( fid, 'floating point example: %07.2f\n', floating_point );
fprintf( fid, 'integer data:  %d\n', integer );
%all three data types on one line (separated by commas)
fprintf( fid, '%s,%7.2f,%d\n', string, floating_point, integer );
%end of file salutation (notice use of double single-quote mark)
fprintf( fid, 'That''s all folks\n');

%don't forget to close the file when you are finished writing data
fclose( fid );

%
%Examine the contexts of the 'textoutput.txt' file we justed created.
%
type textoutput.txt
