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
    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?
     
    Frank Bormann, Dec 27, 2006
    #1
    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)');
    fclose(f);

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

    % 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
     
    daniel_nordlund_1982, Dec 27, 2006
    #2
    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.