Access of global array element not allowed

1 view (last 30 days)
Hey everyone,
in my code i implemented a class (called Job) which has get and set functions (inherited from hgsetget). I initialised a global array of Job objects (called JobStack) and want to set the properties of every single element within this array, but MATLAB throws an error 'Setting the 'id' property of the 'Job' class is not allowed.'
In my Job class I defined property access=public and also method access=public, so in my eyes it should be allowed to use the set routine.
Thanks for your help!
PS: some parts of the code:
classdef Job < hgsetget
properties (Access = public)
id;
end
methods
%constructor
function obj = Job(varargin)
...
end
%set functions
function set.id(obj,id_)
if (id_ < 0)
error('Id must be positive')
else
obj.id = id_;
end
end
end
end
The part where I initialise my global array of Jobs:
global JobStack;
for i=(1:N)
JobStack(i) = Job();
end
And the loop in which I try to set the id value:
for i=(1:N)
get(JobStack(i), 'id');
end

Answers (2)

Geoff Hayes
Geoff Hayes on 23 Jan 2015
Andreas - the code you reference is to get the id
for i=(1:N)
get(JobStack(i), 'id');
end
and not set the id. If I use a cell array to create the JobStack as
global JobStack;
N=25;
for k=(1:N)
JobStack{k} = Job();
end
and then set the id as
for k=(1:N)
set(JobStack{k},'id',k);
end
it seems to work fine. Try the above and see what happens!

Andreas Mießen
Andreas Mießen on 23 Jan 2015
Oh sorry, it was not the get, instead the line
set(JobStack(i), 'id', i);
If I change the round brackets to braces, it doesn't work at all. And it should be an array of Jobs and not a cell array.

Community Treasure Hunt

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

Start Hunting!