Access different property from set method in class

18 views (last 30 days)
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

Accepted Answer

Steven Lord
Steven Lord on 11 Apr 2019
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.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!