Change struct property values in a class
4 ビュー (過去 30 日間)
古いコメントを表示
Hi, I have a problem with using a struct as property of a value class. It seems I cannot change the values of fields (or add new fields).
The simplest example is as follows:
classdef testclass
properties
struct_prop=struct;
end
methods
function obj = change_struct_prop(obj, val)
obj.struct_prop.field1 = val;
end
end
end
Trying to change the struct_prop doesn't work:
>> a=testclass;
>> a.change_struct_prop(2);
>> a.struct_prop
ans =
field1: []
>>
Is there any way to manipulate a class property that is a struct (without having to define a separate class for it)? Defining the struct_prop fields in the initialisation function didn't help either.
Thanks a lot,
Olf
0 件のコメント
採用された回答
per isakson
2014 年 10 月 28 日
編集済み: per isakson
2014 年 10 月 29 日
Replace
classdef testclass
by
classdef testclass < handle
 
In response to the comment
If a value class, replace
>> a.change_struct_prop(2);
by
>> a = a.change_struct_prop(2);
and read the help on the comparison of value and handle classes
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Scope Variables and Generate Names についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!