Saveobj and Loadobj for arrays of objects

조회 수: 14 (최근 30일)
Matt J
Matt J 2025년 5월 21일
댓글: Matt J 2025년 5월 27일
I am trying to customize the save() and load() process for a classdef. Cuurrently, I am using old-style saveobj() and loadobj() methods, as I am still trying to get familiar with the newer approach.
Unlike most class methods, calling saveobj and load obj on an array of objects,
saveobj(objArray)
loadobj(objArray)
does not result in the entirety of objArray being passed to the user-provided code. Instead, there is some background Matlab process that invokes them one element at a time, equivalent to,
for i=1:numel(objArray)
saveobj(objArray(i))
loadobj(objArray(i))
end
However, my saveobj() and loadobj needs to know things about the entire array being saved, and calling them one element at a time hides this information. Is there any way to overcome this problem? As I said, I am still getting acquainted with the newer custom serialization and deserialization tools. Is there any chance that could hold a solution?
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 5월 21일
I wonder about saveobj({objArray}) ?
Matt J
Matt J 2025년 5월 22일
편집: Matt J 2025년 5월 22일
That would invoke the builtin saveobj method of the cell class, which I don't believe we can overload.
I could create a user-defined container class for the objArray and make a saveobj method for that, but that means I can't just use load() and save() as directly as with other user-defined objects. I would have to go through the extra effort of wrapping/unwrapping objArray in the container.

댓글을 달려면 로그인하십시오.

채택된 답변

Benjamin Kraus
Benjamin Kraus 2025년 5월 27일
편집: Benjamin Kraus 2025년 5월 27일
It sounds like you may benefit from switching to an object that "fakes" its size. Some examples of objects that do this (that ship with core MATLAB) include table, string, datetime, duration, and others.
For example: a "vector" of duration objects is really a scalar object that "fakes" its size, you can see this by calling builtin to access the size:
d = hours(1:10);
size(d)
ans = 1×2
1 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
builtin('size',d)
ans = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can see that while size reports [1x10], this is really a single object that pretends to be a vector.
The reason this would help you is that because this is really a scalar object, calling save on that "vector" will only call saveobj once with the entire "vector".
Making an object like this used to be quite complicated, but MATLAB recently (R2021b) introduced matlab.mixin.indexing.RedefinesParen which makes this substantially easier. In fact, the example on the documentation page of how to use the class is an example of a class that is a scalar object that behaves like a matrix. You can start with that base example and add a saveobj and loadobj method to test how this works and if it will satisfy your requirements.
  댓글 수: 2
Benjamin Kraus
Benjamin Kraus 2025년 5월 27일
편집: Benjamin Kraus 2025년 5월 27일
As an aside, the newer approach you mention is awesome and worth learning about, but from the perspective of arrays of objects, the behavior is the same as the old approach.
Matt J
Matt J 2025년 5월 27일
Thanks @Benjamin Kraus, that does sound like the right approach.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by