I was able to workaround this, as apparently the genericWho function is called when the matfile is created and loads all the variable information needed to load files. I created the property
partialWho = struct();
in matlab.io.MatFile and modified genericWho to:
function varargout = genericWho(obj, fcnHan, fcnName, varargin)
nargoutchk(0,1);
validateFirstArgIsObj(obj, fcnName);
varargout = cell(1,nargout);
for k = find(ismember(varargin,fields(obj.partialWho)))'
varargout{k} = obj.partialWho.(varargin{k});
end
if isempty(varargin)||any(~ismember(varargin,fields(obj.partialWho)))
if ~sourceExists(obj)
% Use '~' to represent a variable name that is not possible
% to generate empty return value of the right type.
[varargout{1:nargout}] = fcnHan('~');
else
if isempty(~ismember(varargin,fields(obj.partialWho)))
inds = 1:nargout;
else
inds = ~ismember(varargin,fields(obj.partialWho));
end
[varargout{inds}] = fcnHan('-file', ...
obj.Properties.Source, varargin{~ismember(varargin,fields(obj.partialWho))});
end
end
for i=1:numel(varargout)
for j = find(~ismember({varargout{i}.name},fields(obj.partialWho)))
obj.partialWho.(varargout{i}(j).name)=varargout{i}(j);
end
end
end
This takes 31s when the matfile object is created, but only 0.04 seconds for the subsequent loading call. I haven't done enough testing to guarantee that it doesn't break at other points.
I'm surprised to see so many slow loading problems with matfile for large files, which is the main use case for matfiles in practice. It appears to have been rolled out before being ready for broad usage.