the matlab way of preprocessor macros
조회 수: 13 (최근 30일)
이전 댓글 표시
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
Bruno Luong
2018년 9월 21일
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.
답변 (2개)
Wil Koenen
2018년 9월 21일
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)
댓글 수: 0
Walter Roberson
2012년 6월 5일
You can use static methods of a class.
You can define UNCONNECTED as a function that returns 24 .
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!