MATLAB: Setting Access Properties for Structured Variable

classooppropertiesset get

Hi all,
I'm developing a class in which I am defining a structured variable (to keep the class neater) but want to define different Set/Get properties for the variables within the structure. As an example:
properties (SetAccess = protected)
...
tables = struct('sqlQuery',table(),'trainingTable',table(),...
'numericTrainingTable',table());
end
Where I want sqlQuery to have SetAccess = Protected while i want trainingTable and numericTrainingTable to be (SetAccess = public, GetAccess = public).
I hope I explained this clearly enough, is this possible?
Thanks for the help.
AB

Best Answer

No, it's not possible to set access attributes on structure fields. You can only do that on class members.
Moreover, I doubt that you want SetAccess = Protected on any field of the structure, because the effect would be that your class (even protected/private methods of the class) would not be able to modify the value of these fields. Only the structure itself could. What you want instead is only give set access to your class.
The workaround is fairly simple: use a class instead of a structure. The content of the class can be very basic to replicate the working of a structure:
classdef ClassTable %to be used as a member of main class
properties (Access = public)
trainingTable = table();
numericTrainingTable = table();
end
properties (SetAccess = ?MainClass) %replace MainClass by name of your class. Only MainClass can set sqlSquery
sqlQuery = table();
end
end
classdef MainClass
properties (SetAccess = protected)
...
tables = ClassTable();
end
end