Access different property from set method in class

조회 수: 18 (최근 30일)
Nikolai
Nikolai 2019년 4월 11일
답변: Steven Lord 2019년 4월 11일
Hello all,
I have a class that has a user-controlled property to turn on/off the use of gpuArrays. I want to access this property in a "set" method for a different property. That way, I can immediately store the new property in GPU memory upon calling the set method. However, I see the warning: "A set method for a non-Dependent property should not access another property ('useGpu')". Is there a smarter way to have access to the useGpu property? The use of a global variable seems sloppy to me.
My set method looks as follows:
function obj = set.smaps(obj,val)
val = single(val);
if obj.useGpu
obj.smaps = gpuArray(val);
end
end

채택된 답변

Steven Lord
Steven Lord 2019년 4월 11일
As long as the get method for the useGpu property doesn't try to set the smaps property (leading to an infinite loop) and as long as your class doesn't satisfy both the criteria in the first section of this documentation page I think you're probably okay.
This is a warning alerting you to the possibility that you may be doing something problematic. If you understand what you're doing you can suppress the message for the line of code where the warning is reported, see the "Adjust Code Analyzer Message Indicators and Messages" section on that documentation page.
It is not an error alerting you to the certainty that what you're doing won't work (trying to name a variable 2x or something similar.) The only way to suppress errors and have your code work is to correct the syntactically invalid code.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by