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.
0 个评论
回答(1 个)
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!