Visdiff .html save simplification/collapse

12 Ansichten (letzte 30 Tage)
Merih Dikbas
Merih Dikbas am 5 Aug. 2021
Beantwortet: Sameer am 12 Sep. 2024
File=fopen('comparison.html','w');
Comp=visdiff('text1.txt','text2.txt');
fprintf(File,'%s',Comp);
fclose(File);
I have a .html file with all lines of the texts. I want .html file where only the differences are printed. how can I collapse the results that printed?
Thanks.

Antworten (1)

Sameer
Sameer am 12 Sep. 2024
Hi Merih
From my understanding, you want to create an HTML file that highlights only the differences between two text files, "text1.txt" and "text2.txt". You want to collapse the results such that only the differing lines are displayed, rather than all lines.
The "visdiff" function is more suited for visual inspection rather than programmatic extraction of differences. However, you can use other methods to achieve this.
Here's a possible approach:
% Read the contents of the files
text1 = fileread('text1.txt');
text2 = fileread('text2.txt');
% Split the contents into lines
lines1 = strsplit(text1, '\n');
lines2 = strsplit(text2, '\n');
% Initialize a cell array to hold differences
differences = {};
% Find differences
maxLines = max(length(lines1), length(lines2));
for i = 1:maxLines
if i > length(lines1)
differences{end+1} = sprintf('<p style="color:red;">Line %d: %s</p>', i, lines2{i});
elseif i > length(lines2)
differences{end+1} = sprintf('<p style="color:blue;">Line %d: %s</p>', i, lines1{i});
elseif ~strcmp(lines1{i}, lines2{i})
differences{end+1} = sprintf('<p style="color:blue;">Line %d (text1): %s</p>', i, lines1{i});
differences{end+1} = sprintf('<p style="color:red;">Line %d (text2): %s</p>', i, lines2{i});
end
end
% Open file for writing
File = fopen('comparison.html', 'w');
% Write the differences to the HTML file
fprintf(File, '<html><body>\n');
fprintf(File, '%s\n', differences{:});
fprintf(File, '</body></html>\n');
% Close the file
fclose(File);
Hope this helps!

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by