How to set a private property
Show older comments
Ok so I'm used to c++, and in c++ a private variable (property in matlab) can be accesed by every method in that class, I'm trying to do the same here in the constructor, I want it to establish some default values if no arguments have been provided, this is my code:
classdef modulo
properties (Access=private)
A
B
R
actuadores
end
methods
function obj=modulo(BB,Actuadores)
if nargin<1
obj.B=[0,0,0];
end
if nargin<2
obj.actuadores=0;
else
obj.B=BB;
obj.actuadores=Actuadores;
end
end
end
end
But then, I do
smth=modulo
or
smth=modulo([4 3 2],5)
and I expect:
smth =
modulo with properties:
A: []
B: [0 0 0]
R: []
actuadores: 0
or
smth =
modulo with properties:
A: []
B: [4 3 2]
R: []
actuadores: 5
But instead I get:
smth =
modulo with no properties.
It works as intended when I don't set any private attributes, but how can I access those properties if I want them to be private?
Accepted Answer
More Answers (0)
Categories
Find more on Create System Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!