function a = fileinNC(fname, NC)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fileinNC:
%
% Reads NC columns of real numbers from file and returns them as 
% a 2D array with size NC x NLINES (NLINES is the number of lines 
% in the file).
%
% Uses built in functions: fopen, fscanf, fclose.
%
% INPUTS
%
%    fname (character string): Name of input file.
%
% OUTPUTS 
%
%    a: Array of numbers read from file, size NC x NLINES.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   % Set default return value (empty).
   a = [];

   % Return if fname is not a string.
   if ~ ischar(fname) 
      fprintf('filein2: First argument ''fname'' must be a string\n');
      return;
   end

   % Attempt to open file for read.
   fid = fopen(fname, 'r');

   % Return if fopen failed.
   if fid < 0 
      fprintf('filein2: Error opening ''%s'' for read.\n', fname);
      return;
   end

   % Type 'help fscanf' for more information on how the following
   % works ...
   [a, count] = fscanf(fid,'%g',[NC,Inf]);

   % Close the file.
   fclose(fid);
end
