Comparison of excel sheet data and output should be excel sheet for matched and mismatched data using simulink model.
58 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (1)
Tejas
le 19 Août 2024 à 1:48
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:
6 commentaires
Tejas
le 23 Août 2024 à 4:45
Modifié(e) : Tejas
le 23 Août 2024 à 4:49
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1758349/image.png)
test2.xlsx
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1758354/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1758359/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1758364/image.png)
Voir également
Catégories
En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!