loadobj() always called from matfile() regardless of subsref
조회 수: 3 (최근 30일)
이전 댓글 표시
MATLAB calls loadobj() whenever you access the contents of a saved matfile, if there's an object saved there. Example:
classdef toomanyloadobj
methods
function obj = toomanyloadobj()
disp('->constructor');
end
end
methods (Static)
function obj = loadobj(s)
obj = toomanyloadobj(); % must return obj with this class
disp('->loadobj');
end
end
end
Script to run:
clc;
x = 1;
disp('new instance');
obj = toomanyloadobj();
disp('save mat');
save('toomanyloadobj.mat','obj','x');
disp('matfile');
mat = matfile('toomanyloadobj.mat');
disp('look at x (but it also looks at everything else)');
mat.x
disp('load only x');
s = load('toomanyloadobj.mat','x');
Output:
new instance
->constructor
save mat
matfile
->constructor
->loadobj
look at x (but it also looks at everything else)
->constructor
->loadobj
ans =
1
load only x
So even if I just want the value of "x" from the matfile, it reruns loadobj() every time I access mat. This is a huge problem if I have several objects saved and each has its own (possibly complicated and time-consuming) loadobj().
As you can see, if I directly load "x" using load(..,'x'), it works fine, but there are advantages to using matfile() which I'd rather retain, like the ability to load variables and write to the mat file directly.
Any workaround?
댓글 수: 6
답변 (1개)
Hari
2023년 10월 16일
Hi Roys,
I understand that you are facing an issue where the “loadobj” function is being called every time you access the contents of a saved “matfile”, even if you only want to retrieve a specific variable.
To address this, you can use the “load” function with the specific variable name to directly load only the desired variable without triggering the “loadobj” function.
Here is a sample code for accessing a variable without triggering the “loadobj” function:
% Create a sample class with loadobj() function
classdef toomanyloadobj
methods
function obj = toomanyloadobj()
disp('->constructor');
end
end
methods (Static)
function obj = loadobj(s)
obj = toomanyloadobj(); % must return obj with this class
disp('->loadobj');
end
end
end
% Save the object and variable to a matfile
x = 15;
obj = toomanyloadobj();
save('toomanyloadobj.mat', 'obj', 'x');
% Load only the variable 'x' using load()
s = load('toomanyloadobj.mat', 'x');
disp("loaded first time " + s.x);
s = load('toomanyloadobj.mat', 'x');
disp("loaded second time " + s.x);
By using “load('toomanyloadobj.mat', 'x')” you can directly load the variable 'x' without invoking the “loadobj” function. Please note that while using the “load” function with the specific variable name can bypass this issue, it may not be possible to prevent “loadobj” from being called when using “matfile”.
Here is the output observed with the above modification (“loadobj” is not invoked every time):
->constructor
loaded first time 15
loaded second time 15
Refer to the documentation of “load” and “matfile” functions in MATLAB for more information.
Hope this helps!
댓글 수: 1
Walter Roberson
2023년 10월 16일
The specific requirement is to be able to use matfile without the object being constructed before it is requested. Using load() with a specific variable name does not solve the problem
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!