Info

This question is closed. Reopen it to edit or answer.

Why does my Class take up less room than my variables?

1 view (last 30 days)
Gavin
Gavin on 5 Feb 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
I'm dealing with huge quantities of data to create a database. I decided to do this using MATLAB, and to define some classes such that I have better control over my data as it is loaded, what functions are called when it is loaded etc.
I spotted an interesting thing yesterday, though. That is when I save a number of variables in a .mat file, it occupies about 4times as much space in MB as when I save the same quantity of data as an object.
For example, the following variables saved as a .mat file occupies around ~4MB of space.
data - 120 x 12000 double channelname - 120 x 40 char channelunits - 120 x 20 char startDate - 1 x 8 char startTime - 1 x 8 char sampleRate - 1 x 1 double
However, if I save it as an object of a class which I've defined, the mat file only uses around ~1MB of space.
Given that I have around 40,000 of these files, this reduction in memory-usage is great. It means when I've carried out the initial stage and sorted it into the form I want it, I'll be using up only about 0.04TB of Hard Drive as opposed to 0.16TB.
Below is a simplification of the class definition:
classdef myData<handle;
properties
sDate='';sTime='';fHz=[];
cName='';cUnit='';data=[];
end
methods
function obj=myData(sDate,sTime,fHz,cName,cUnit,data)
obj.sDate=sDate;
obj.sTime=sTime;
obj.fHz=fHz;
obj.cName=cName;
obj.cUnit=cUnit;
obj.data=data;
end
end
end
Like I said, the above is a simplification, but the general jist of how the data is stored is there.
I am by no means complaining. This is good news, this reduction in storage. I just want to make sure I'm not doing anything stupid. It wouldn't seem so, in that when loading the .mat file with the object, all the data seems to be there.
Hopefully someone can give me some insight?
Gav
  1 Comment
per isakson
per isakson on 6 Feb 2013
Edited: per isakson on 6 Feb 2013
Which version of mat-file do you refer to? Version, v7, uses compression and the size depends on the values. E.g
data = ones( 120, 12000 );
is stored in a very small mat-file

Answers (2)

Daniel Shub
Daniel Shub on 6 Feb 2013
If any of the properties of your class are handle classes (e.g., is data of class myData), then when you save the object I think you might only save the handle and not the actual data.

per isakson
per isakson on 6 Feb 2013
Edited: per isakson on 6 Feb 2013
I made a little experiment with R2012a and the default mat-file version, i.e. v7.0. It uses compression.
  • created an object, mr, and saved it
  • created six variables with the "same" (different(?) seed to rand) data and saved those
the two mat-files are of the same size.
>> sad = dir( 'c:\temp\*rand.mat' );
>> sad.bytes
ans =
10895679
ans =
10895643
>>
========
>> clear
>> mr = my_rand
mr =
my_rand handle
Properties:
data: [120x12000 double]
name: [120x40 char]
unit: [120x20 char]
date: 'zzzzzzzz'
time: 'zzzzzzzz'
rate: 1
Methods, Events, Superclasses
>>
where
classdef my_rand < handle
properties
data
name
unit
date
time
rate
end
methods
function this = my_rand()
this.data = rand( 120, 12000 );
this.name = repmat( 'z', [120,40] );
this.unit = repmat( 'z', [120,20] );
this.date = repmat( 'z', [ 1, 8] );
this.time = repmat( 'z', [ 1, 8] );
this.rate = 1;
end
end
end

This question is closed.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!