How to use for loop to set properties' attributes in MATLAB class?

7 visualizaciones (últimos 30 días)
laha_M
laha_M el 18 de Nov. de 2020
Comentada: laha_M el 19 de Nov. de 2020
I want to use the following loop to set properties' attributtes inside a class. Essentially it needs to store a struct as a class attribute. But when I put the following inside the class properties, its showing error.
Total_Grids = 100;
Grid(Total_Grids) = struct();
G = 1;
for i = 1:this.XGrid
for j = 1:this.YGrid
Grid(G).X = (this.GridSize/2)+ (j-1)*(this.GridSize); % x pos of each grid centre
Grid(G).Y = (this.GridSize/2)+ (i-1)*(this.GridSize); % y pos of each grid centre
G = G + 1;
end
end
Profits = randi([0,20],100,1); % profit of each grid, 100 grids are there
G = 1;
for i = 1:Total_Grids
Grid(G).Profit = Profits(G); % stored in Grid structure
G = G+1;
end
How and where to write it inside the class definition? I see normally the attributes are written like the following. Its not even using semicolon at the end of each attribute. How do I write a loop like the above here?
Thanks.
classdef CartPoleEnvironment < rl.env.MATLABEnvironment
%CARTPOLEENVIRONMENT: Template for defining custom environment in MATLAB.
%% Properties (set properties' attributes accordingly)
properties
% Specify and initialize environment's necessary properties
% Acceleration due to gravity in m/s^2
Gravity = 9.8
% Mass of the cart
CartMass = 1.0
%....and so on
end
  3 comentarios
laha_M
laha_M el 18 de Nov. de 2020
This is how I have written
properties
Total_Grids = 100; %this.XGrid*this.YGrid;
Grid(Total_Grids) = struct();
G = 1;
for i = 1:10
for j = 1:10
Grid(G).X = (this.GridSize/2)+ (j-1)*(this.GridSize); % x pos of each grid centre
Grid(G).Y = (this.GridSize/2)+ (i-1)*(this.GridSize); % y pos of each grid centre
G = G + 1;
end
end
Profits = randi([0,20],100,1); % profit of each grid, 100 grids are there
for i = 1:Total_Grids
Grid(G).Profit = Profits(G); % stored in Grid structure
G = G+1;
end
end
For which I get the following error:
Illegal use of reserved keyword
"for".
I am attaching one screenshot. Seems such syntax is not allowed.
laha_M
laha_M el 18 de Nov. de 2020
I understand that there was one confusing part because I was using 'this.something' even before creating the object.
But when I remove them and implement the for loop like this (without using any 'this')
for i = 1:10
for j = 1:10
Grid(G).X = 10 + (j-1)*20; % x pos of each grid centre
Grid(G).Y = 10+ (i-1)*20; % y pos of each grid centre
G = G + 1;
end
end
I get the same error and warnings.
Thanks.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 18 de Nov. de 2020
Editada: Matt J el 18 de Nov. de 2020
You cannot put a for loop or other complex code inside a properties block. If you wish, you can set a default value for a property with the following function call syntax, as long as the function defaultProp1() requires no inputs. Otherwise, you must initialize the property in the constructor.
classdef myclass
properties
prop1=defaultProp1()
end
end
function value=defaultProp1()
...
end
Here defaultProp1() can be defined anywhere that is visible to the classdef file, but a good place to put it would be in the classdef file as a class-related function.
  3 comentarios

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by