Visdiff .html save simplification/collapse

1 次查看(过去 30 天)
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 中查找有关 Develop 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