使用 MATLAB 比较文件 [英] Compare files with MATLAB

查看:38
本文介绍了使用 MATLAB 比较文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 MATLAB 比较两个文件(逐行)(*.xml、.m、.txt、...等).

I would like to know how could I compare two files (line by line) (*.xml, .m,.txt,...etc) using MATLAB.

file1 = 'toto.xml';
file2 = 'titi.xml';

我正在寻找一个 MATLAB 函数(通过命令行),它返回真/假或差异列表.

I'm looking for a MATLAB function (by command line) that returns me true/false or list of diff.

推荐答案

如果您在 Windows 中,您可以使用 MATLAB 的 system 命令和 fc:

You can use MATLAB's system command with fc if you are in Windows:

file_name_1 = 'file1.txt';
file_name_2 = 'file2.txt';

[status,result] = system(['fc ' file_name_1 ' ' file_name_2]);

这里 status 如果文件相等则为 0,否则为 1.此外,如果文件不同,result 会有差异结果.

Here status will be 0 if files are equal and 1 if not. Furthermore result will have the diff result if files differ.

对于其他操作系统,您可以使用类似的命令,例如 Unix 中的 cmp 而不是 fc.

For other operating systems you can use similar commands such as cmp in Unix instead of fc.

更新:

为了跨平台兼容性,您可以尝试以下方法:

For cross-platform compatibility you may try the following:

file_1 = javaObject('java.io.File', 'file1.txt');
file_2 = javaObject('java.io.File', 'file2.txt');
is_equal = javaMethod('contentEquals','org.apache.commons.io.FileUtils',...
                      file_1, file_2)

这篇关于使用 MATLAB 比较文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆