Effacer les filtres
Effacer les filtres

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)
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?

Réponses (1)

Tejas
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
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
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
Divyashree il y a environ 2 heures
Hello Tejas,
Thank you so much. It worked.
I updated the code as mentioned above. But the output file is in different path.
Suppose if I give 'C:\Users\Divya\Documents\MATLAB\folder1', the excel sheet is not getting generated in this path. It is getting generated in the different folder, like 'C:\Users\Divya\Documents\MATLAB\folder2'.
After giving the command addpath 'C:\Users\Divya\Documents\MATLAB\folder1', the output file is getting generated in different path.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by