How to store this type of data with unknown number of them?

5 views (last 30 days)
I have a N by 3 column data, each row, I need to store some additional information like the below:
empty []
[1, 2, 31]
[45]
[89, 37]
These are the station numbers from another file.
What's the best way to store these values, to have the smallest footprint (file size)? Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2025
Let us experiment
N = 100;
A = randi(9,N,1);
B = randi(9,N,1);
C = randi(9,N,1);
D = cell(N,1);
for K = 1 : N
D{K} = randi(99,1,randi(5,1));
end
T1 = {A, B, C, D};
T2 = table(A, B, C, D);
T3 = table([A,B,C], D);
T4 = cell(N,1);
for K = 1 : N
T4{K} = [A(K), B(K), C(K), D{K}];
end
whos T1 T2 T3 T4
Name Size Bytes Class Attributes T1 1x4 17256 cell T2 100x4 18479 table T3 100x2 18001 table T4 100x1 16776 cell
save T1.mat T1
save T2.mat T2
save T3.mat T3
save T4.mat T4
ls -l T*.mat
-rw-r--r-- 1 matlab matlabgroup 975 Jun 10 17:27 T1.mat -rw-r--r-- 1 matlab matlabgroup 2189 Jun 10 17:27 T2.mat -rw-r--r-- 1 matlab matlabgroup 2181 Jun 10 17:27 T3.mat -rw-r--r-- 1 matlab matlabgroup 934 Jun 10 17:27 T4.mat
So the smallest memory size is a cell array of rows of variable size, with the first three entries in each row being a 1 x 3 row extract and the remaining entries in the row being variable entries.
Note that this is not the best storage arrangement if you are needing to extract one of the three columns from it.
The smallest file size is also the same cell array just mentioned. But the difference in file size compared to the 1 x 4 cell array is quite small; one suspects that the difference might almost be accidental, with it being plausible that with different data that the 1 x 4 cell array just might come out slightly smaller.
The versions T1 and T2 are suitable for the case where the datatypes of everything might be different.
The version T3 is suitable for the case where the datatypes of the N x 3 are all the same but the datatypes of the variable data might be different.
The version T4 depends on all of the datatypes being the same.
  1 Comment
Leon
Leon on 10 Jun 2025
Awesome. Thank you so much for demonstrating the leanest way to store them. Interesting to learn that cell array is a very effective approach of saving information.

Sign in to comment.

More Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!