%%Formatted File Output

% Recall: we can use save to save variables in the workspace.
% They're saved in MATLAB's own format or they're saved in a text
% file where the matrices are stacked on top of each other

% A lot of times we want to save data in a specific format that is not
% either of these options. For example, want header information, such
% as labels for each column of the data

% We can do this using extra functionality of fprintf

% First, we need to open a file for writing.

%fileID = fopen(filename, permission);

% First argument is the name of the file we want to open.
% Second argument sets what type of access we have to the file.
% Use 'r' to open the file for reading. Use 'w' to open the file for
% writing (this overwrites any data inside the file currently).
% Use 'a' to open the file for appending (adding to the end of the file).

% When we're done using a file, we can close it using
%fclose(fileID);

% Once we've opened the file for writing, we can write to it using fprintf.
% This works almost exactly the same way as writing to the command window
% except that there is a new argument at the front of fprintf

%fprintf(fileID, formatSpec, A1, A2 ...)

%Recall:
%formatSpec describes the format of the output
%Can contain literal text, escape characters, places to insert values
%To insert values - use a conversion character
% %d - integer (signed)
% %f - floating point number
% %c - single characters
% %s - single strings
%Before conversion character, we can have
%  Field width, e.g., %12d
%  Precision, e.g., %10.4f means a field width of 10, 4 spots to the right
%  of the decimal point
%  Flags, e.g., '-' does left justify the text

%Example:
fileID = fopen('mytext.txt', 'w');
name = 'Mike';
fprintf(fileID,'Hello %s\n', name);
fclose(fileID);

%Formatted file example

%Intended format:
% <two word title>
% N=<number of measurements>
%
% hh:mm:ss
% dd-mmm-yyyy
% <matrix of data values>
% hh:mm:ss
% dd-mmm-yyyy
% <matrix of data values>
%
% etc.