【问题标题】:Importing Constants from .cpp/.h files to matlab将常量从 .cpp/.h 文件导入 matlab
【发布时间】:2014-04-09 22:24:22
【问题描述】:

所以我有一个这样定义的 .h 文件

#ifndef CONSTANTS_H
#define CONSTANTS_H

#include "math.h"


//CALA = CA/LA, CRLR = CR/LR
#define CALA 0.2
#define CRLR 25.0

//self-propulsion and friction
#define ALPHA .15
#define BETA  .05  

//Evolution simulation size
#define STUDYSIZE 100
#define STUDYLENGTH 1
 .....
#define INITIAL_CONDITION true

#endif

有没有办法把我这里定义的常量导入matlab变量。

【问题讨论】:

    标签: c++ matlab


    【解决方案1】:

    试试这个 -

    %%// Read in data
    imp_data = importdata(CPP_H_FILE,'\n');
    
    %%// Remove the leading and trailing whitespaces
    imp_data = cellstr(strtrim(char(imp_data)));
    
    %%// Split into strings
    split1 = regexp(imp_data,'\s','Split');
    
    %%// Process to evaluate the variables into MATLAB
    for k = 1:size(split1,1)
        if strcmp(char(split1{k,1}(:,1)),'#define') && numel(split1{k,1})>2
            val_str = strtok(char(split1{k,1}(:,end)), '/');
            evalstr = strcat(char(split1{k,1}(:,2)),'=',val_str);
            evalc(evalstr);
        end
    end
    

    【讨论】:

    • 不完全。这是我的整个文件。检查顶部
    • @JohnWu 已修复,请查看!问题在于尾随空格。
    猜你喜欢
    • 2013-02-23
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多