Visdiff .html save simplification/collapse

조회 수: 9 (최근 30일)
Merih Dikbas
Merih Dikbas 2021년 8월 5일
답변: Sameer 2024년 9월 12일
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.

답변 (1개)

Sameer
Sameer 2024년 9월 12일
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!

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by