%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Illustrates use of fscanf to input data from a file.  
%
% Requires use of additional built in functions:
%
%    fopen:  Opens file 
%    fclose: Closes file 
%
% Note that there are many different ways that fscanf can be called,
% some of which allow data stored in a file in a particularly 
% organized fashion (e.g. as columns) to be read with little coding 
% effort.
%
% This script illustrates a general technique that can be used 
% in cases where the data to be read is not necessarily structued,
% e.g. as an array.
% 
% Refer to online documentation or seek help from the instructor
% should you wish to read from files as part of your term project, 
% and you are having difficulty doing so.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First, open 'tfprintf.dat' for reading.
% 
%    First (string) argument to fopen specifies name of file.
%    Second (string) argument to fopen specifies type of access,
%       'r' means read.
%
%    Return value is an integer 'file descriptor' that is then
%    used in 'fprintf' calls.  If return value is < 0, then 
%    open failed, as could happen, for example, if file does not 
%    exist, or if you don't have permission to read the file.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('tfprintf.dat', 'r');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Bail out if can't open file for write ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if fid < 0 
   fprintf('Could not open ''tfprintf.dat'' for read.\n');
   fprintf('Try running script ''tfprintf'' first.\n');
   return;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Arguments to fscanf:
%
%    First:  file descriptor
%    Second: template string (specifies integer values)
%    Third:  size of array that is to be read --- in this case
%            [3,Inf] means 'read all values in the file into a 
%            matrix with 3 rows and as many columns as necessary'
%       
% Return value:
% 
%     Array of values read
%        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vals = fscanf(fid, '%g %g %g', [3,Inf]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Transpose vals to match the appearance of the data in the file (i.e.
% so the matrix has 3 columns and a number of rows = to the number 
% of lines in the file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vals = vals';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the number of lines in the file (number of columns in vals)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
szvals = size(vals);
nlines = szvals(1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output the data with descriptive information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1 : nlines
   fprintf('k=%d   k^2=%d   k^3=%d\n', vals(k,1), vals(k,2), vals(k,3));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Close file ... 
%
% Note that  the file descriptor, NOT the file name is the argument
% passed to fclose ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fclose(fid);
