在MATLAB中比较文件 [英] compare files within MATLAB
问题描述
可能重复:
使用MATLAB比较文件
Possible Duplicate:
Compare files with MATLAB
我想使用MATLAB比较2个txt文件并在文件不相等的情况下打印差异
I would like to compare 2 txt files using MATLAB and print the diff if files aren't equal
我发现visdiff是图形工具,但我想知道是否有一些MATLAB函数进行这种比较?
I found visdiff which is graphical tool but I would like to know if there are some MATLAB function doing such comparison ?
如果文件之间存在差异,则仅打印+或-文件
if there are diff between files print only + or - files
谢谢
推荐答案
在linux/unix中,可以在Matlab中使用system()
来使用bash diff
. (相关文章)
In linux/unix, you can use bash diff
, using system()
in matlab. (related article)
它是这样的:
[content_differs, printout] = system('diff --side-by-side --left-column file1 file2');
如果file1和file2具有相同的内容,则
content_differs
为0,printout
为字符串.您可以逐行访问其数据(可以在matlab中使用split或通过管道传递其他命令).
'
可以根据中间的字符来分析差异.正如我所观察到的,("出于某种原因没有区别.<",>"和"|"是指添加和更改的行.
content_differs
is 0 if file1 and file2 have the same content, printout
is a string. You can access its the data line-by-line (you can use split in matlab or pipe other commands).
'
The differences can be parsed according to the character in the middle. As I have observed, "(" means no difference for some reason. "<", ">" and "|" refer to additions and changed lines.
(您在diff上有太多选择,也可以显示常见内容-查看此链接以获取详细信息)
(You have soo many options with diff to display common content too -- check out this link for details)
更新:
一个简单的文件解析脚本,其中显示了所有常见部分.
A simple parsing script for your file which displays all the common parts.
file1 = 'your_file.m'
file2 = 'your_other_file.m';
[is_diff,output] = system(['diff --side-by-side --left-column ',file1,' ',file2]);
lines = regexp(output, '\n', 'split');
for i=1:(length(lines)-1)
line = lines{i};
if line(end) == '(' % common part
disp( line(1:(end-1)) );
end
end
这篇关于在MATLAB中比较文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!