Cell Array Size and Saving
Show older comments
Hi,
I wanted to save my quite complex and large class to a file and experienced a much larger filesize than I would have expected. So I examined which parts were driving the size. I was quite surprised how simple cell arrays of string consumed overdimensional space. Is there any easy way to avoid this?
Here my MWE:
Names50 = cell(50,1);
Names2 = cell(2,1);
for i=1:length(Names2)
Names{i} = 'a';
end % for i
for i=1:length(Names50)
Names{i} = 'b';
end % for i
When I check for saving size with a small routine I found, I get quite confusing results:
getSize(Names2) --> 228
getSize(Names50) --> 5700
getSize(Names2{1}) --> 2
The single element is just 2 bytes, while a cell array of 2*2 bytes is 228, or even 5700 if there are 50 rows. Is the overhead so unproportional large in cell arrays? Can that somehow be avoided when saving?
Thanks in advance
Best
Sven
P.S.: Codes for getSize:
function [ bytes ] = getSize( variable )
props = properties(variable);
if size(props, 1) < 1, bytes = whos(varname(variable)); bytes = bytes.bytes;
else %code of Dmitry
bytes = 0;
for ii=1:length(props)
currentProperty = getfield(variable, char(props(ii)));
s = whos(varname(currentProperty));
fprintf('Property: %s : %d bytes\n',props{ii},s.bytes)
bytes = bytes + s.bytes;
end
end
end
function [ name ] = varname( ~ )
name = inputname(1);
end
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!