0

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

Community
  • 1
  • 1
lola
  • 5,649
  • 11
  • 49
  • 61
  • Can you be clearer about what you mean when you say 'give the diff'? My first instinct would be to use the output of the `diff` unix tool (or equivalent on Windows). Do you just have to display it to the user or what? – devrobf Nov 15 '12 at 16:11
  • 2
    `S = visdiff(file1, file2)` actually returns the HTML report as a string. Would that suffice? – Eitan T Nov 15 '12 at 16:13
  • No, as I said I would like to use command line not graphical tool – lola Nov 15 '12 at 16:14
  • It won't bring a GUI if you assign the output value to a variable. – Eitan T Nov 15 '12 at 16:20
  • How do you want it to print the diff? Do you want just the line numbers? You need to elaborate on your requirements... – Eitan T Nov 15 '12 at 16:25
  • print diff betwwen files : print missing lines ,diffrenet lines – lola Nov 15 '12 at 16:31
  • 2
    I think this is a case of an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to do with that output, and why isn't `visdiff` sufficient? – Eitan T Nov 15 '12 at 16:36
  • You could save the visdiff result, or parse it to identify which lines are added or missing. Or if you want to check whether there is any difference at all you can first compare a file with itself, check the lenght of the visdiff report, and see how long it is after comparing with the other file. May require compensation for file name lenght difference. – Dennis Jaheruddin Nov 15 '12 at 22:44
  • 1
    Haven't you already asked this question? http://stackoverflow.com/questions/9315878/compare-files-with-matlab – dinkelk Nov 16 '12 at 00:10
  • I think I can use visdiff with return,but is there an easy way to parse content ? – lola Nov 16 '12 at 07:53
  • I have shown one in my updated answer. – Barney Szabolcs Nov 27 '12 at 02:14

1 Answers1

0

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
Community
  • 1
  • 1
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91