How to keep a class property constant after it's specified the first time

29 views (last 30 days)
I have defined a class property that shuffles a given set of inputs when it is called, such that each class instantiation would have a unique, randomized array in that property. So far so good. However, I am also trying to store the array corresponding to each particular instantiation, so that I could retrieve that information later on.
Currently, every time I call the property (through get), I obtain a different shuffled version, which is not what I need. For example:
classdef Stimulus < AuditoryStimulus
properties
carrierRange = 1e3 : 1e3 : 1e4;
end
properties (Dependent = true, SetAccess = private)
stimulus
frequencySeries
end
methods
function frequencySeries = get.frequencySeries(obj)
frequencySeries = obj.carrierRange(randperm(length(obj.carrierRange)));
end
function stimulus = get.stimulus(obj)
freqs = obj.frequencySeries;
% build the stimulus...
end
I have tried with constant properties, but I am not able to initialize it with the output of a function. I have also tried with static methods, but that is not what I want either, because I need each particular instantiation to be unique.
Could anyone please tell me what I am missing and/or point out some relevant references? Thanks a lot!
  2 Comments
jgg
jgg on 10 Dec 2015
Edited: jgg on 10 Dec 2015
It seems like the issue is that:
function frequencySeries = get.frequencySeries(obj)
frequencySeries = frequencySeries(randperm(length(frequencySeries)));
end
creates a new permutation each time it is called. I think you could get around this by only calling the randperm(length(frequencySeries)) function once, when the object is created. Then, this would just have to return the private value frequencySeries instead.
Am I understanding this correctly?

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 10 Dec 2015
I don't have much experience with using OOP with MATLAB (or really OOP at all to be frank), but I think what you want to do is initialize the random variable as part of the class constructor method, that only gets called when the object is instantiated.
  2 Comments
paula
paula on 10 Dec 2015
So, in case anyone else has a similar question, this is the structure of my implementation:
classdef testStimulus < AuditoryStimulus
properties
carrierRange = 1e3 : 1e3 : 1e4;
end
properties (Dependent = true, SetAccess = private)
stimulus
end
properties (Dependent = false, SetAccess = private)
carriers
end
methods
function obj = testStimulus()
obj = obj@AuditoryStimulus;
obj.carriers = randomizefrequencies(obj);
end
function stimulus = get.stimulus(obj)
% do other things... (common method)
end
end
end
function frequencySeries = randomizefrequencies(obj)
frequencySeries = obj.carrierRange(randperm(length(obj.carrierRange)));
end

Sign in to comment.

More Answers (1)

Adam
Adam on 10 Dec 2015
Edited: Adam on 10 Dec 2015
The accepted answer by the cyclist is basically correct, though in addition I would add that what you are looking for is
properties (SetAccess = immutable)
carriers
end
You can add the Dependent = false if you like, but I tend to leave that off and only add it when it is true for readability purposes. Even then I just put 'Dependent' rather than 'Dependent = true', but that is just a stylistic aside!
The 'immutable' property access specifier is for exactly the case you are asking about - it defines that the property can only be set from the constructor and then even the class itself cannot set it afterwards.
Your solution works fine in realistic circumstances, but if you were working on, for example, a complex class with a team of people then just putting
properties (SetAccess = private)
...
end
does not limit the property to never being changed after it is set. The class' own methods are still allowed to change it whenever they like so for tight programming practices you should make it immutable because that is a clear statement that the property should never be changed. Then others working on the code would also know this is your clear intention.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!