%%%%%%%%%%%%%%%%%%%%% Open an Input File %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

infile = 'inputfile.txt'; % Name of file is stored into "infile".

fptrn = fopen(infile, 'rt'); % Pointer to input file that is opened for reading.

if (fptrn == -1)
  fprintf('Error - could not open file %s\n', infile);
  exit;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%% Reading a Line from Input File %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

firstline = fgetl(fptrn);  % Read line from file, discard newline character.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%% Open an Ouput File %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

outfile = 'outputfile.txt';

fpout = fopen(outfile, 'wt'); % Pointer to output file that is opened for writing.

if (fpout == -1)
  fprintf('Error - could not open file %s\n', outfile);
  exit;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%% Writing to Output File %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf(fpout,'Total = %d\n', total); % Writing a number to the output file.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%% Reading User Input from MATLAB Command Line %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fnumber = input('Enter number of files: '); % Request user input.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
