Comparing two textual Files
Show older comments
I have written a function to compare two text files. When I run the test of the function,I do not get the expected results. The problem is that when I run the test, I get "Not Necessary to compare files" printed in the results of all the test cases.
% Initialize outputs testStatus = 1; testMessage = {};
% Open, read each line (row) and close the first text file FID = fopen(File1, 'r'); % Opens the first (reference) text file File1 for reading Data = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', ''); % Reads File1 data into a cell array Data lines1 = Data{1}; % Returns the first line of cell array of strings Data to lines1 fclose(FID); % Close the opened first file
% Open, read each line (row) and close the second text file FID = fopen(File2, 'r'); Data = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', ''); lines2 = Data{1}; fclose(FID);
% Get the number of elements in cellOneData1 and cellOneData2 numElOfLines1 = numel(lines1); % Number of elements in array cellOneData1 numElOfLines2 = numel(lines2); % Number of elements in array cellOneData2
% Compare the content the cell arrays cellOneData1 and cellOneData2 for i=1:numElOfLines1
if i <= numElOfLines2
if strcmp(lines1{i},lines2{i})== 1
else
testStatus = 0;
outText = ['Line ' num2str(i) ':' ' ' lines1{i} ' Is different from: ' lines2{i}];
testMessage{end+1} = sprintf(outText);
end
else
if i > numElOfLines2 % If number of lines in lines1 is more than that of lines2
testStatus = 0;
outText = 'Not Necessary to Compare the Files';
testMessage{end+1} = sprintf(outText);
return;
end
end
end
Answers (0)
Categories
Find more on Text Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!