For Programmers: Free Programming Magazines  


Home > Archive > Matlab > December 2006 > Use of C defines in MATLAB









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Use of C defines in MATLAB
Frank Bormann

2006-12-27, 4:19 am

Hi,

is there any way to use constant defines from C header files in
MATLAB?

For example:

mybits.h:
#define MSG_INFO 1
#define MSG_WARN 2
#define MSG_ERR 3
#define MSG_FAIL 4

Can I somehow import and use these constants in MATLAB so I only
would have to change mybits.h if I want to reassign different error
numbers?
daniel_nordlund_1982@hotmail.com

2006-12-27, 8:17 am

Frank Bormann skrev:
> Hi,
>
> is there any way to use constant defines from C header files in
> MATLAB?
>
> For example:
>
> mybits.h:
> #define MSG_INFO 1
> #define MSG_WARN 2
> #define MSG_ERR 3
> #define MSG_FAIL 4
>
> Can I somehow import and use these constants in MATLAB so I only
> would have to change mybits.h if I want to reassign different error
> numbers?


Yes. Manually parse the file mybits.h and use the eval() function.

% load mybits.h
f = fopen('mybits.h','rt');
s = char(fread(f)');
fclose(f);

% parse
v = regexp(s,'#define\s+(\S+)\s+(\S+)','toke
ns');

% eval
for e = v
eval([e{1}{1} '=' e{1}{2} ';']);
end

If you have a newer version of Matlab you should of course use a
dynamic regexp instead of a for loop.

Daniel

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com