Property visibility behaves as hidden when using breakpoints and "property acces list"
6 次查看(过去 30 天)
显示 更早的评论
Dear Matlab Community,
I am currently working on a project using classes for which I need to restrict the accessibility of certain properties to a set of classes. To check the value of these properties, I typically place a breakpoint in one of the methods of one of the classes having access to one of these properties. When "in the method", the value of the said property can be accessed from the terminal, showing that the access is well granted to the class the breakpoint is in. Nevertheless, the property cannot be seen when opening the instance in the workspace panel.
Here is a small demo:
BaseClass is used to provide accessibility
classdef BaseClass <handle
end
MainClass stores a property:
classdef MainClass < BaseClass
properties (GetAccess=?BaseClass,SetAccess=immutable)
Property1
end
methods
function obj = MainClass(inputArg1)
obj.Property1=inputArg1;
end
end
end
ParentClass is a subclass of BaseClass and stores a MainClass instance
classdef ParentClass < BaseClass
properties
child MainClass
end
methods
function obj = ParentClass(child)
obj.child = child;
end
function testMethod(obj)
end
end
end
Instances of the classes are then created:
my_main=MainClass(5)
my_parent=ParentClass(my_main);
Now, the property cannot be accessed from the terminal, as expected:
my_parent.child.Property1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1373319/image.png)
But a breakpoint can be placed in the testMethod of the ParentClass. There the property is accessible
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1373324/image.png)
But not "visible":
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1373329/image.png)
It appears that the property access behaves as "Hidden" when in a method of a class listed in the Property Access List. I could not find any information in the documentation on this behaviour. Is there any option to make such properties visible when "in a method" using a breakpoint? It would be very convenient for debugging purposes.
0 个评论
回答(1 个)
Prateekshya
2023-8-10
As per my understanding, within the "testMethod" function, you can see the value of Property1 in the Command Window but not in the Workspace. Assuming that we cannot modify the class definitions and access lists, one possible solution is to use the "assignin" function to store the value of Property1 in a variable in the workspace of your choice.
Here's an example that demonstrates how to achieve this:
function testMethod(obj)
assignin('base','temp',obj.child.Property1);
end
In this code, the assignin function is used to store the current value of Property1 from the obj.child object into a variable named 'temp' in the base workspace. You can adjust the workspace name as well as the function inside which you will add this line of code as per your requirement.
By executing this line within your method, the value of Property1 will be accessible outside the method in the specified workspace. You can then access and use the stored value as needed in your MATLAB code.
For further information on the "assignin" function, you can refer to the MATLAB documentation: https://in.mathworks.com/help/matlab/ref/assignin.html
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Customize Object Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!