Compare files with MATLAB

后端 未结 5 1425
时光取名叫无心
时光取名叫无心 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:18

    First you can read both files by lines:

    fid1 = fopen(file1, 'r');
    fid2 = fopen(file2, 'r');
    
    lines1 = textscan(fid1,'%s','delimiter','\n');
    lines2 = textscan(fid2,'%s','delimiter','\n');
    lines1 = lines1{1};
    lines2 = lines2{1};
    
    
    fclose(fid1);
    fclose(fid2);
    

    You will have 2 cell arrays lines1 and lines2. You can compare the whole arrays with

    tf = isequal(lines1,lines2);
    

    Comparing lines is not so obvious and depends on your need. What you want to do if number of lines is different? For example, to find which lines from file2 exist in file1 (independently of order) you can do:

    [idx1 idx2] = ismember(lines1,lines2);
    idx2(idx2==0) = [];
    

    idx1 will be logical index representing lines in file1 that have the same lines in file2. idx2 will be numeric (position) index of where those lines located in file2 (the first occurrence).

    If the number of lines are the same:

    idx_same_lines = strcmp(lines1,lines2);
    
    0 讨论(0)
  • 2021-01-22 07:18

    In case that you have a file (or a path) with spaces (like c:\my folder\myfile.m), wrap the file name with quotation mark (") in the DOS command. Note that the apostrophe is still needed:

    file_name_1 = 'file 1.txt';
    file_name_2 = 'file 2.txt';
    
    [status,result] = system(['fc ', '"', file_name_1, '" "', file_name_2, '"']);
    

    Alternatively, you can put the quotation mark as soon as you define the variable of the file name:

    file_name_1 = '"file 1.txt"';
    file_name_2 = '"file 2.txt"';
    
    [status,result] = system(['fc ' file_name_1 ' ' file_name_2]);
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-22 07:26

    AFAIK the only diffing tool in MATLAB is visdiff which does not return any information, but displays a window with both files side-by-side and highlights the different lines.

    0 讨论(0)
  • 2021-01-22 07:39

    first change the text file to string, then use strcmp function of matlab.

    text1 = fileread('test.txt');
    text2 = fileread('testcp.txt');
    strcmp(text1, text2)
    
    0 讨论(0)
提交回复
热议问题