How can i create constant in a .m archive and use it for the creation of new objects?

3 次查看(过去 30 天)
Hi, I have an archive named Constantes.m
%% Logica
TRUE=1;
FALSE=0;
%% Colores
AZUL=[0 0 1];
ROJO=[1 0 0];
BLANCO=[1 1 1];
%% Tablero
COLUMNAS=8;
FILAS=8;
%% Juego
TURNO_IA=[1 0 0];
TURNO_JUGADOR=[0 0 1];
And i want to use those variables into my class archives, for example
classdef Pieza
properties
Color=BLANCO;
Rey=FALSE;
Direccion=0;
end
methods
function obj = Pieza(Color,ColorDeJugador)
%Constructor de Pieza (Color, Color Elegido)
obj.Color=Color;
Rey=FALSE;
if isequal(Color,ColorDeJugador)
Direccion=-1;
elseif isequal(Color,BLANCO);
Direccion=0;
else
Direccion=1;
end
end
function obj = Coronar(obj)
%Parametro del objeto Pieza Rey a 1(verdadero)
obj.Rey=TRUE;
end
end
end
how is it done?
Thanks

采纳的回答

Walter Roberson
Walter Roberson 2020-12-3

更多回答(1 个)

Rik
Rik 2020-12-3
编辑:Rik 2020-12-3
You could turn your script into a function. If you store all variables as fields of a struct you can use something like this to select a variable:
Color=getConstant('BLANCO');
In getConstant.m:
output=constants.(input);
This way you can even take advantage of persistent variables to generate the struct only once.

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by