Saving with handle objects

15 ビュー (過去 30 日間)
Jason Climer
Jason Climer 2019 年 5 月 17 日
コメント済み: Steven Lord 2019 年 5 月 17 日
I'm trying to create some handle classes that are nested together in a struct - something like this:
classdef A < handle
properties
parent
data
end
methods
function obj = Data(parent,raw_data)
obj.parent = parent;
obj.raw_data = raw_data;
end
end
methods (Abstract)
out = get(obj)
end
end
Instances of A are compiled in another handle class:
classdef B < handle
properties
data = struct;
epoch = [0 inf];
end
methods
function load_C(obj,field_name,varargin)
obj.data.(field_name) = C(obj,varargin{:});% C is an instance of an A class
end
function out = get(obj,field_name)
out = obj.data.(field_name).get();
end
end
end
When I try to save such objects, the struct data looses all of its fields! This is particularly wierd to me because in other classes I've made where the handle objects are not stored in a struct and are just properties of the class, they are saved.
  3 件のコメント
Jason Climer
Jason Climer 2019 年 5 月 17 日
Here's the ugly workaround:
classdef B < Handle
properties
data = struct;
fields = {};
data_obs = {};
end
methods
function b = saveobj(obj)
obj.fields = fields(obj.data);
obj.data_obs = cellfun(@(x)obj.data.(x),obj.fields,'UniformOutput',false);
b = obj;
end
end
methods (Static)
function b = loadobj(a)
for j=find(~ismember(a.fields,fields(a.data)))'
a.data.(a.fields{j}) = a.data_obs{j};
end
b = a;
end
end
Steven Lord
Steven Lord 2019 年 5 月 17 日
Can you show us the code you use to construct the object that you're trying to save as well as the actual save call you're executing? Having a complete end-to-end example will help us investigate what's happening.
In addition, one of the comments in your code is:
% C is an instance of an A class
Can you show us the definition of the C class or the C function?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by