Can I detect array index inside setter method?

6 ビュー (過去 30 日間)
Konrad Warner
Konrad Warner 2020 年 7 月 17 日
コメント済み: Konrad Warner 2020 年 7 月 19 日
Is there a way to detect the index/position of the setted value inside a new defined setter method?
I mean, if I call
obj.Prop(idx) = val;
that
function set.Prop(obj,val)
if % idx > 5
val = ...;
end
obj.Prop = val;
end
does different calculations on specific indices.
THX
In MATLAB documentation is a short description I don't really understand________________________________________
Access Methods and Properties Containing Arrays
You can use array indexing with properties that contain arrays without interfering with property set and get methods.
For indexed reference:
val = obj.PropName(n);
MATLAB calls the get method to get the referenced value.
For indexed assignment:
obj.PropName(n) = val;
MATLAB:
  • Invokes the get method to get the property value
  • Performs the indexed assignment on the returned property
  • Passes the new property value to the set method
  2 件のコメント
Image Analyst
Image Analyst 2020 年 7 月 18 日
I don't understand the question. In essence you say you want to "detect the index/position of the setted value" but then you say you did this:
obj.Prop(idx) = val;
so is idx NOT the index/position of the value you wanted to set? You already know this index value or else you could not have used it like that. So why do you think you need to "detect" it?
Konrad Warner
Konrad Warner 2020 年 7 月 18 日
編集済み: Konrad Warner 2020 年 7 月 18 日
Yes a little difficult to explain.
Because Prop is stored in a static data object, an external class of properties that are shared among instances of the class that calls
obj.Prop(idx) = val;
or rather:
obj.StaticData.Prop(idx) = val;
So the array gets 'filled' from a certain number of existing objects during runtime. The index - like a instance ID - is known but should trigger some calculations on the hole array as soon as its value gets passed in.
But maybe you are right. I can just can use a normal class method rather then a setter method inside the static data object and pass in both, value and idx.

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

採用された回答

Tommy
Tommy 2020 年 7 月 18 日
"For indexed assignment:
obj.PropName(n) = val;
MATLAB:
  • Invokes the get method to get the property value
  • Performs the indexed assignment on the returned property
  • Passes the new property value to the set method"
If I am understanding this correctly, then
obj.PropName(n) = val;
is essentially similar to
myProp = obj.PropName; % get method
myProp(n) = val;
obj.PropName = myProp; % set method
so that neither the get method nor the set method ever sees n.
You could compare obj.Prop to val within your set method before you set obj.Prop so that you can determine which value is being changed, but that doesn't seem like a very good solution. You could also look into overloading subsasgn for your object, which might do the trick (requiring that you explicitly call subsasgn within your class rather than use '()' and '.', see here for explanation). Though I hope I'm overlooking a simpler solution.
  1 件のコメント
Konrad Warner
Konrad Warner 2020 年 7 月 19 日
Yes thanks, essentially I wanted to find n inside the setter method and your explanation made it more clear to me how these mehods handle arrays... even though it doesn't seem posible directly for setters there should be some workarounds for me

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by