Access different property from set method in class

18 次查看(过去 30 天)
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