Comparison of excel sheet data and output should be excel sheet for matched and mismatched data using simulink model.
Show older comments
I have two excel sheets, Data1 and Data2. I need to compare both the excel sheet and atlast I need to get excel sheet as an output for matched data and mismatched data using simulink model. Tell me how to read the excel sheet, compare it and and how to get the excel sheet as an output?
Answers (1)
Tejas
on 19 Aug 2024
Hello Divyashree,
To compare two Excel files in Simulink, create a .M script that reads the Excel files and extracts matched and mismatched data. This script can then be called from a 'MATLAB Function Block' within Simulink.
Below is an example code snippet that demonstrates how to load and compare the Excel files. This example performs a row-wise comparison and creates an Excel file with two sheets: one containing matched rows and the other containing mismatched rows.
function compareExcelSheets()
% Read the excel sheets
data1 = readtable('Data1.xlsx');
data2 = readtable('Data2.xlsx');
% Specify names of columns to consider for comparison
columnsToCompare = {'Column_name_1', 'Column_name_2', 'Column_name_3'}; % Replace with your actual column names
matchedData = [];
mismatchedData = [];
% Compare the data row by row
for i = 1:min(height(data1), height(data2))
isMatched = true;
for col = columnsToCompare
colName = col{1};
if ~isequal(data1{i, colName}, data2{i, colName})
isMatched = false;
break;
end
end
% Add row to matched or mismatched data
if isMatched
matchedData = [matchedData; data1(i, :)];
else
mismatchedData = [mismatchedData; data1(i, :)];
end
end
outputFileName = 'ComparisonResult.xlsx';
writetable(matchedData, outputFileName, 'Sheet', 'MatchedData');
writetable(mismatchedData, outputFileName, 'Sheet', 'MismatchedData');
end
In the 'MATLAB Function Block' within Simulink, add this code to call the script. Make sure the .M script is on MATLAB path.
coder.extrinsic('compareExcelSheets');
compareExcelSheets();
For more details on the solution, kindly refer to the documentation below:
10 Comments
Divyashree
on 19 Aug 2024
Edited: Walter Roberson
on 20 Aug 2024
Tejas
on 20 Aug 2024
Hello Divyashree,
The reason the data is being added to a single column might be related to how it is stored in the Excel sheets. Could you please share the Excel sheets with me? This way, I can modify the code accordingly.
Divyashree
on 20 Aug 2024
Moved: Walter Roberson
on 20 Aug 2024
Walter Roberson
on 20 Aug 2024
matchedData = [];
mismatchedData = [];
Those are plain arrays
if isMatched
matchedData = [matchedData; data1{i, :}];
else
mismatchedData = [mismatchedData; data1{i, :}];
end
The {} indexing indexes the table and returns a plain array. So you have [;] operations between a plain array and plain array, which will give a plain array in return.
matchedMatrix = cell2mat(matchedData);
mismatchedMatrix = cell2mat(mismatchedData);
cell2mat applied to a plain array would be an error.
.... possibly your arrays are stored as cell arrays inside the table. If so then you would be [;] together cell arrays, getting out a cell. cell2mat() of that would "flatten" the cell arrays, probably given a character array as a result. But writing a character array would give a block of text as a result, with no delimeter between the entries.
I suspect that you need to get rid of the cell2mat() calls.
Hello Divyashree,
Based on the Excel file provided earlier, I have created two sample files to test the code. Below are screenshots of these files:
test1.xlsx

test2.xlsx

Here is the updated code for the 'compareExcelSheets' function.
function compareExcelSheets()
data1 = readtable('test1.xlsx');
data2 = readtable('test2.xlsx');
columnsToCompare = {'data1', 'data2', 'data3','data4'};
matchedData = table();
mismatchedData = table();
for i = 1:min(height(data1), height(data2))
isMatched = true;
for col = columnsToCompare
colName = col{1};
if ~isequal(data1{i, colName}, data2{i, colName})
isMatched = false;
break;
end
end
if isMatched
matchedData = [matchedData; data1(i, :)];
else
mismatchedData = [mismatchedData; data1(i, :)];
mismatchedData = [mismatchedData; data2(i, :)];
end
end
outputFileName = 'ComparisonResult.xlsx';
writetable(matchedData, outputFileName, 'Sheet', 'MatchedData');
writetable(mismatchedData, outputFileName, 'Sheet', 'MismatchedData');
end
Additionally, here are the screenshots of the 'ComparisonResult.xlsx' file:


Divyashree
on 25 Aug 2024
Walter Roberson
on 27 Aug 2024
data1 = readtable('test1.xlsx');
data2 = readtable('test2.xlsx');
readtable will search along the MATLAB path looking for test1.xlsx and test2.xlsx . Those two are potentially in different folders. So you would need something like
filename1 = 'test1.xlsx';
filename2 = 'test2.xlsx';
w1 = which(filename1);
if isempty(w1)
error('file not found: "%s"', filename1);
end
w2 = which(filename2);
if isempty(w2)
error('file not found: "%s"', filename2);
end
d1 = fileparts(w1);
d2 = fileparts(w2);
if ~strcmp(d1, d2)
error('test1 is in a different directory than test2, "%s" vs "%s". Cannot decide where to save results', d1, d2);
end
savefolder = d1;
%...
%...
outputFileName = fullfile(savefolder, 'ComparisonResult.xlsx');
This will have the effect of saving ComparisonResult.xlsx in whatever directory test1.xlsx happens to live in (provided that it is the same directory as test2.xlsx )
It sure would be easier if the files were in a consistent location relative to running the script, instead of having to fish around looking for the files.
Divyashree
on 27 Aug 2024
Walter Roberson
on 30 Aug 2024
filename1 = 'test1.xlsx';
filename2 = 'test2.xlsx';
w1 = which(filename1);
if isempty(w1)
error('file not found: "%s"', filename1);
end
w2 = which(filename2);
if isempty(w2)
error('file not found: "%s"', filename2);
end
d1 = fileparts(w1);
d2 = fileparts(w2);
if ~strcmp(d1, d2)
error('test1 is in a different directory than test2, "%s" vs "%s". Cannot decide where to save results', d1, d2);
end
savefolder = d1;
columnsToCompare = {'data1', 'data2', 'data3','data4'};
matchedData = table();
mismatchedData = table();
for i = 1:min(height(data1), height(data2))
isMatched = true;
for col = columnsToCompare
colName = col{1};
if ~isequal(data1{i, colName}, data2{i, colName})
isMatched = false;
break;
end
end
if isMatched
matchedData = [matchedData; data1(i, :)];
else
mismatchedData = [mismatchedData; data1(i, :)];
mismatchedData = [mismatchedData; data2(i, :)];
end
end
outputFileName = fullfile(savefolder, 'ComparisonResult.xlsx');
writetable(matchedData, outputFileName, 'Sheet', 'MatchedData');
writetable(mismatchedData, outputFileName, 'Sheet', 'MismatchedData');
Divyashree
on 2 Sep 2024
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!