Saveobj and Loadobj for arrays of objects

9 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 21 May 2025
I wonder about saveobj({objArray}) ?
Matt J
Matt J on 22 May 2025
Edited: Matt J on 22 May 2025
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.

Sign in to comment.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 27 May 2025
Edited: Benjamin Kraus on 27 May 2025
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 Comments
Benjamin Kraus
Benjamin Kraus on 27 May 2025
Edited: Benjamin Kraus on 27 May 2025
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 on 27 May 2025
Thanks @Benjamin Kraus, that does sound like the right approach.

Sign in to comment.

More Answers (0)

Categories

Find more on Object Save and Load in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!