5
votes

I have an object from some class I wrote in Matlab. When I use "whos" command to determine its size, it is roughly 720,000 Bytes. When I save it in a .mat file it take roughly 75MB. What is going on?

Is there an efficient way of saving and loading an object in Matlab?

EDIT: Here is a list of the properties and their size

            CT_COL: 2
            p: 5
            d: 10
            n: 37
            N: 20
          idx: [20x1 double]
           Am: [4-D double]
            X: [4-D double]
            y: [37x1 double]
        KGram: [20x20 double]
       reWave: []
          rpw: 2
         grps: [1x37 double]
        exIDX: [1 4 5 6 13]
          nCV: 100
        prIDX: [100x6 double]
        trIDX: [100x26 double]
            U: [5x100 double]
            V: [10x100 double]
            B: [20x100 double]
         Yhat: [37x100 double]
          Lam: [100x1 double]
    peakRatio: [37x1 double]

Both Am and X are 20x10x5x37 arrays (of double)

By the way, the property "reWave" used to hold the handle of a method of another object. I thought that might not be a good idea (and might be causing this), so I have removed any mention of it from the class definition. But it seems to somehow appear in the set of properties. (Even after I have issued "clear classes".)

EDIT2: I am using the command save('uvXbMod1.mat','ob') to save just the object. Here comes the puzzling: When I use the command whos -file uvXbMod1.mat to see what is saved inside the file, it shows

Name      Size             Bytes   Class        Attributes

ob        1x1              680512  uvXbModel   

(This is for another instance, not the one mentioned above.) What else is put in the .mat file that makes it that big?

EDIT3: OK... The problem seemed to be two inline function handles I stored in two protected properties. Just these two, @(X) median(abs(X),2) and @median . The handles themselves were just a few bytes in size, and I assumed that since they are inline functions, they should just be stored along with their one-line definitions as text (?). But apparently that does not happen, it causes a huge amount of other things to be stored along (which doesn't seem that strange after the fact ...)

1
Could you be a little more descriptive about the object? What is its type and what are its dimensions? - Ryan J. Smith
That object definitely seems consistent with your report of 720 kB or so. Is it possible you're saving the entire workspace and not just this object? - Ryan J. Smith
What is the command you are using for the save? It should be something like save('myfile.mat', 'myobject'). If you leave off the second parameter, then, as @RyanJ.Smith suggested, you would be saving your entire workspace - which could easily be 75M. A .mat file is typically much smaller than the size reported by whos since it's compressed. - Floris
How big are those 4D arrays? :) - Rody Oldenhuis
I am afraid that without being able to reproduce the issue we won't be of much help. I would recommend either to share the code or contact TMW support to retain privacy. - Oleg

1 Answers

5
votes

Here is the issue that I have found with my code: The problem was two inline anonymous function handles I stored in two protected properties. Just these two, @(X) median(abs(X),2) and @median.

The handles themselves were just a few bytes in size, and I assumed that since they are inline anonymous functions, they should just be stored along with their one-line definitions as text. But apparently that does not happen, and it causes a huge amount of other things to be stored along.