Leila
Leila

Reputation: 206

How do you a make a class property read-only, from outside the class, in 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

Upvotes: 4

Views: 2526

Answers (2)

informaton
informaton

Reputation: 1482

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.

Upvotes: 3

matlabgui
matlabgui

Reputation: 5672

Have a look at the documentation for the properties - specifically the SetAccess property.

Upvotes: 0

Related Questions