compare files within MATLAB [duplicate]

谁说胖子不能爱 提交于 2019-12-08 12:41:11

问题


Possible Duplicate:
Compare files with MATLAB

I would like to compare 2 txt files using MATLAB and print the diff if files aren't equal

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

thanks


回答1:


In linux/unix, you can use bash diff, using system() in matlab. (related article)

It goes like this:

[content_differs, printout] = system('diff --side-by-side --left-column file1 file2');

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.

(You have soo many options with diff to display common content too -- check out this link for details)

UPDATE:

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


来源:https://stackoverflow.com/questions/13401538/compare-files-within-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!