Visdiff .html save simplification/collapse

1 回表示 (過去 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 Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by