Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all differences between .mat files

I am looking for a way to list the differences between two .mat files, something that can be usefull for many people.

Though I searched everywhere I could think of, I have not found anything that meets my requirements:

  1. Pick 2 mat files
  2. Find the differences
  3. Save them properly

The closest I have come is visdiff. As long as I stay within matlab, it will allow me to browse the differences, but when I save the result it only shows me the top level.


Here is a simplified example of what my files typically look like:

a = 6;
b.c.d = 7;
b.c.e = 'x';
save f1
f = a;
clear a
b.c.e = 'y';
save f2
visdiff('f1.mat','f2.mat')

If I click here on b, I can find the difference. However if I run this and use 'file>save', I am not able to click on b. Thus I still don't know what has been changed.

Note: I don't have Simulink


Hence my question is:

How can I show all differences between 2 mat files to someone without Matlab


Here are the answers that I personally consider to be most suitable for different situations:

  • Answer for users with Simulink
  • General answer
  • Answer displaying all value differences
like image 762
Dennis Jaheruddin Avatar asked Oct 02 '13 09:10

Dennis Jaheruddin


People also ask

How do I compare codes in MATLAB?

To open the Comparison Tool, go to the Home tab, and in the File section, click Compare. Then select the files or folders to compare. You can also compare a file that is open in the Editor. On the Editor or Live Editor tab, in the File section, select Compare.


1 Answers

Find all differences between mat files without MATLAB?

You can find the differences between HDF5 based .mat files with the HDF5 Tools.

Example

Let me shorten your MATLAB example and assume you create two mat files with

clear ; a = 6 ; b.c = 'hello' ; save -v7.3 f1
clear ; a = 7 ; b.e = 'world' ; save -v7.3 f2

Outside MATLAB use

h5ls -v -r f1.mat

to get a listing about the kind of data included f1.mat:

Opened "f1.mat" with sec2 driver.
/                        Group
    Location:  1:96
    Links:     1
/a                       Dataset {1/1, 1/1}
    Attribute: MATLAB_class scalar
        Type:      6-byte null-terminated ASCII string
        Data:  "double"
    Location:  1:2576
    Links:     1
    Storage:   8 logical bytes, 8 allocated bytes, 100.00% utilization
    Type:      native double
/b                       Group
    Attribute: MATLAB_class scalar
        Type:      6-byte null-terminated ASCII string
        Data:  "struct"
    Location:  1:800
    Links:     1
/b/c                     Dataset {5/5, 1/1}
    Attribute: H5PATH scalar
        Type:      2-byte null-terminated ASCII string
        Data:  "/b"
    Attribute: MATLAB_class scalar
        Type:      4-byte null-terminated ASCII string
        Data:  "char"
    Attribute: MATLAB_int_decode scalar
        Type:      native int
        Data:  2
    Location:  1:1832
    Links:     1
    Storage:   10 logical bytes, 10 allocated bytes, 100.00% utilization
    Type:      native unsigned short

Use of

h5ls -d -r f1.mat

returns the values of the stored data:

/                        Group
/a                       Dataset {1, 1}
    Data:
        (0,0) 6
/b                       Group
/b/c                     Dataset {5, 1}
    Data:
        (0,0) 104, 101, 108, 108, 111

The data 104, 101, 108, 108, 111 represents the word hello, which can be seen with

h5ls -d -r f1.mat | tail -1 | awk '{FS=",";printf("%c%c%c%c%c \n",$2,$3,$4,$5,$6)}'

You can get the same listing for f2.mat and compare the two outputs with the tool of your choice.

Comparison also works directly with HDF5 Tools. To compare the two numbers a from both files use

h5diff -r f1.mat f2.mat /a

which will show you the values and their difference

dataset: </a> and </a>
size:           [1x1]           [1x1]
position        a               a               difference          
------------------------------------------------------------
[ 0 0 ]          6               7               1              
1 differences found
attribute: <MATLAB_class of </a>> and <MATLAB_class of </a>>
0 differences found

Remarks

There are a few more commands and options in the HDF5 Tools, which may help to get your real problem solved.

Binary distributions are available for Linux and Windows from The HDF Group. For OS X you can get them installed via MacPorts. If needed there is also a GUI: HDFView.

like image 135
BHF Avatar answered Oct 06 '22 04:10

BHF