Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you a make a class property read-only, from outside the class, in MATLAB?

Tags:

oop

matlab

How can I make a property of a MATLAB class such that it can be read from outside the class but cannot be set from outside the class? For example, I want the sensor property (below) to only be settable from within the Data class, while also being readable from outside it.

classdef Data   
   properties
     sensor;
   end
end
like image 632
Leila Avatar asked Sep 06 '25 03:09

Leila


1 Answers

classdef Data
    properties(SetAccess=protected, GetAccess=public)
       sensor;
    end
end

You can use SetAccess=private instead, if you don't want inheriting classes to have writeable access either.

The default behavior for SetAccess and GetAccess is public, so you don't need to explicitly state GetAccess=public here, but it does not hurt.

like image 97
informaton Avatar answered Sep 08 '25 00:09

informaton