Comparison of excel sheet data and output should be excel sheet for matched and mismatched data using simulink model.

13 Ansichten (letzte 30 Tage)
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?

Antworten (1)

Tejas
Tejas am 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 Kommentare
Walter Roberson
Walter Roberson am 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');

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by