MATLAB Answers


Enums with first element set to zero - Consistency between Matlab and C (EML)

Asked by Alex Bogias on 18 May 2018 at 11:41

HI all,

I am used to defining enums in 'C' in the following manner:

typedef enum = {
ElementA = 0,
ElementB = 1,
ElementC = 2,
ElementD = 3,
} enumName;

Again in 'C' I define and operated arrays always using an enum to avoid any unexpected errors.

for(i=ElementA; i<LAST; i++ )
arrayname[i]= 1+1;

As I am sure you are aware Matlab starts with arrays at element one, whereas in 'C' it is element zero. I am looking for some solution that can 'easily' bridge the two differences. I am trying to avoid having a magic number in my code (in Matlab or 'C') like '+1'. Is there a way to use'Matlab') 

in the Matlab classdef enumaration file (.m)?

I have a number of Matlab functions that I would like to code generate for an embedded application. I use Matlab as my simulation environment for these functions but as mentioned above the Matlab defined enums need start from 1 in Matlab for the first element to be operated on.

Now I have found that if a define the Maltab enums starting for 1, once I code generate functions which have within them an array using that enum, they point to the wrong array element. To explain this a bit more, it seems that the arrays are code generated starting from zero while the enum is code generated starting from 1 (ie the enums are now offset by +1). This inconsistency causes unexpected results in the code.

As far as code generation goes, I use Simulink to encapsulate the functions in Matlab Function Blocks and place those function blocks within Simulink Function blocks. The Simulink Function blocks are logically grouped in separate Simulink models. Each Simulink model is code generated separately. Stateflow using the C action language is used to 'pull' all these code generated functions together and code an application (for the embedded target).

Any help much appreciated.

Thanks Alex


Sign in to comment.

0 Answers

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today