Compare files with MATLAB

后端 未结 5 1428
时光取名叫无心
时光取名叫无心 2021-01-22 06:36

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\';
         


        
5条回答
  •  清酒与你
    2021-01-22 07:22

    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]);
    

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

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

    UPDATE:

    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)
    

提交回复
热议问题