Use of C defines in MATLAB

Discussion in 'MATLAB' started by Frank Bormann, Dec 27, 2006.

  1. Hi,

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

    For example:

    #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
    Frank Bormann, Dec 27, 2006
    1. Advertisements

  2. Frank Bormann skrev:
    Yes. Manually parse the file mybits.h and use the eval() function.

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

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

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

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

    daniel_nordlund_1982, Dec 27, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.