I am trying to find a nice solution for the following situation:
I have a class:
classdef SomeClass < handle
properties (Access = private)
x
end
methods
function x = getX(this)
x = this.x;
end
end
end
Let x – some data array.
Do I have a possibility to address some element of array like I’d make it in struct:
struct.x(5)
Or do I always have to do like this?:
myClassObj = SomeClass();
x = myClassObj.getX();
x(5)
or create some func getXAt?
Yes. Addressing like that is the normal behavior of a property in a Matlab object. You can just expose the property for reading instead of making it fully
private.Then you can address it like a field on a struct.
There’s no need in Matlab to always make your own accessor functions like you would in Java. You can independently control the read and write access of a property using attributes on it. If you want more complex property access logic, you can define getters and setters using the special
function out = get.x(obj)syntax, and their behavior will apply to property access done with theobj.xsyntax.