Saveobj and Loadobj for arrays of objects

9 ビュー (過去 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 件)

カテゴリ

Help Center および File ExchangeObject Save and Load についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by