• Home
  • Blog
  • Comparing two text files in Matlab

Comparing two text files in Matlab

0 comments

function[testStatus,
testMessage] = compare2TxtFiles(File1, File2)

% DESCRIPTION

%  This functions compares two
text file

%

% INPUTS:

% File1  – Name of the
reference text file

% File2  – Name of the text
file to be compared with the reference text file

%

% OUTPUTS:

% testMessage  – Prints
unmatched results of the comparison

% testStatus  – Returns
logical 1 if files matches and logical 0 otherwise

To
implement the design of a function to compare two textual files, the following
steps are considered

· 
Initialize the outputs

· 
Open, Read and close each
file

· 
Compare the opened files

o  If
files are the same (txtFile1.txt and txtFile2.txt), it prints the message “OK”

o  If
files are different (txtFile1.txt and txtFile3.txt), give a message with the
lines that are different in the both files. For example: if line 1 of both
files are: John is programming is different and Peter is programming,
respectively. It should print “Line 1: John is different from Peter”

o  If
the number of line in the reference text file (txtFile1.txt) is more than that
in the test text file (txtFile4.txt). It prints the message “Not necessary to
compare the files”

PROBLEM

I started writing the function but when tested, I do not get
the expected results. I have attached here the test files.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}