Saving a struct property within a class object

I'm trying to save an instance of a class object I've made for later reference. Some of my properties are structs. I can set and manipulate them as expected, but when I save, the saved object reverts to their default values (or empty if there was no default). Do I need to initialize structs in a special way? Or is there an Attribute I need to set?
This is an example of my class:
classdef testclass
properties
S = struct('d', 0, 'e', 1);
number = 1;
end
methods
function obj = set.S(obj, val)
obj.S.d = val + 1;
obj.S.e = val + 3;
end
end
end
My test output - the save function saves the integer value, but doesn't save changes to the struct:
>> t = testclass
t =
testclass with properties:
S: [1×1 struct]
number: 1
>> t.number = 10;
>> t.S = 3;
>> t.S
ans =
struct with fields:
d: 4
e: 6
>> save test.mat t
>> clear
>> load test.mat
>> t.S
ans =
struct with fields:
d: 0
e: 1
>> t.number
ans =
10
The reason my struct set function is written this way is I'd like to give my function a few parameters to calculate trajectory arrays, and then I save those trajectories for reference. This works fine until I try to save my class object, so I've been saving my structs as separate variables outside the class object, i.e.:
S = t.S; save test.mat t S
Is there a cleaner way to do this instead?

回答 (1 件)

Jeremy
Jeremy 2020 年 3 月 31 日
編集済み: Jeremy 2020 年 3 月 31 日

0 投票

classdef testclass
properties (GetAccess=public, SetAccess=public)
S
number
end
methods
function obj = testclass(num,val)
obj.number = num;
obj.S.d = val + 1;
obj.S.e = val + 3;
end
end
end
>> t = testclass(10,3)
t =
testclass with properties:
S: [1×1 struct]
number: 10
>> t.S
ans =
struct with fields:
d: 4
e: 6
>> save test.mat t
>> clear
>> load test.mat
>> t
t =
testclass with properties:
S: [1×1 struct]
number: 10
>> t.S
ans =
struct with fields:
d: 4
e: 6
Does this help?

6 件のコメント

Jeremy
Jeremy 2020 年 3 月 31 日
Note that you can modify this to allow construction with zero inputs by setting defaults inside your testclass method.
Ameer Hamza
Ameer Hamza 2020 年 3 月 31 日
But this does not answer the original question of OP. The question specifically asked about using a setter for the S variable.
Cyndia Cao
Cyndia Cao 2020 年 3 月 31 日
Does this only work upon construction? In my application, I could define "val" before construction, but I have been changing "val" in order to test its effect on the output, so I'd prefer if it wasn't locked in by construction.
As my code was in the question, it was saving the initial value of the struct - it just wasn't saving any changes to the struct values.
Cyndia Cao
Cyndia Cao 2020 年 3 月 31 日
I'm not attached to using the set function - I tried this as well but when I print t.S it doesn't get updated and I haven't figured out why...
function obj = update_struct(obj, val)
obj.S.d = val + 1;
obj.S.e = val + 3;
end
Jeremy
Jeremy 2020 年 3 月 31 日
You call the class methods on the class instance (object) after creation, e.g. in your example you would call it like
t = testclass
update_struct(t)
See some examples here:
Cyndia Cao
Cyndia Cao 2020 年 3 月 31 日
編集済み: Cyndia Cao 2020 年 4 月 2 日
edit: realized the problem with this syntax is that testclass is a value class so I'd have to say t = t.update_struct(3) in order for the change to propagate, or I could change testclass to a handle class. Regardless, changing testclass to a handle class doesn't help with the save/load issue of the struct.
--------------------------
To be more explicit, this was my updated class code:
classdef testclass
properties (GetAccess=public, SetAccess=public)
S = struct('d', 0, 'e', 1);
number = 1;
end
methods
function obj = update_struct(obj, val)
disp('in update function')
obj.S.d = val + 1;
disp([obj.S.d, val + 1])
obj.S.e = val + 3;
disp([obj.S.e, val + 3])
end
end
end
And my command line test:
>> t = testclass;
>> t.update_struct(3)
in update function
4 4
6 6
ans =
testclass with properties:
S: [1×1 struct]
number: 1
>> t.S
ans =
struct with fields:
d: 0
e: 1
I've used classes before (though more extensively outside MATLAB). The other functions in the classes I've implemented work as I expect, except for these cases of managing structs.

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

カテゴリ

ヘルプ センター および File ExchangeScope Variables and Generate Names についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2020 年 3 月 31 日

編集済み:

2020 年 4 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by