Borrar filtros
Borrar filtros

the matlab way of preprocessor macros

17 visualizaciones (últimos 30 días)
Andreas Lobinger
Andreas Lobinger el 5 de Jun. de 2012
Comentada: Bruno Luong el 21 de Sept. de 2018
To make my code more readable i'd like to move some hard coded values like b_index = find(all_user_state == 24); to something like b_index = find(all_user_state == UNCONNECTED); which i would do in C-style with #define.
What would you recommend in Matlab. I already thought about enum and collect the definitions in a def. struct, but would have then the problem to propagate this in all scopes.
  1 comentario
Bruno Luong
Bruno Luong el 21 de Sept. de 2018
The lack of preprocessing is to me a serious drawback in MATLAB. Define static, persistent variable(s) or in function will slow down greatly when one to access to it compared to a direct hard-code value.

Iniciar sesión para comentar.

Respuestas (2)

Wil Koenen
Wil Koenen el 21 de Sept. de 2018
Define a class containing the definitions, either using constant properties (1) or enumeration values (2).
Example (1)
classdef MyDef
properties(Constant)
UNCONNECTED = 24
CONNECTED = 25
% more definitions
end
end
Use as follows:
b_index = find(all_user_state == MyDef.UNCONNECTED)
Example (2)
classdef MyEnum < int32
enumeration
UNCONNECTED(24)
CONNECTED(25)
% more definitions
end
end
Use similarly:
b_index = find(all_user_state == MyEnum.UNCONNECTED)

Walter Roberson
Walter Roberson el 5 de Jun. de 2012
You can use static methods of a class.
You can define UNCONNECTED as a function that returns 24 .

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by