主题:Matlab与C++混合编程,将Matlab参数从字符串改为double型指针
大家好,我最近在做的项目要用到Matlab与C++混合编程,我之前实现了一个版本,Matlab函数编译成动态库,供C++调用,大概的思路就是C++传几个文件名,然后再Matlab函数里读文件数据,处理数据,再将结果写入指定的文件。
代码如下:
// 调用Matlab导出库函数,利用通过显著性检验的相关系数,根据一定的间隔选取预报因子,并构造预报因子与预报量矩阵
libChooseFathrGridInitialize();
const mwArray mw_cor_ci_gho500_may((const char*)cor_ci_gho500_may);
const mwArray mw_cor_ci_gho500_june((const char*)cor_ci_gho500_june);
const mwArray mw_cor_ci_gho500_july((const char*)cor_ci_gho500_july);
const mwArray mw_gh_an_may((const char*)gh_an_may);
const mwArray mw_gh_an_june((const char*)gh_an_june);
const mwArray mw_gh_an_july((const char*)gh_an_july);
const mwArray mw_fa_thr_10m((const char*)fa_thr_10m);
const mwArray mw_an_num_cig_47yr_545st_drought_son((const char*)an_num_cig_47yr_545st_drought_son);
const mwArray mw_fa_thr_obs((const char*)fa_thr_obs);
choose_fathr_grid(mw_cor_ci_gho500_may, mw_cor_ci_gho500_june, mw_cor_ci_gho500_july,
mw_gh_an_may, mw_gh_an_june, mw_gh_an_july, mw_fa_thr_10m,
mw_an_num_cig_47yr_545st_drought_son, mw_fa_thr_obs);
libChooseFathrGridTerminate();
这个也能满足要求,但是在Matlab中来回读写数据感觉很慢,我想改成在C++里读写数据,将传给Matlab函数的参数改成double型的指针,请问诸位,我要怎么修改Matlab函数的接口,以及它内部的变量呢?对Matlab不是很熟悉,希望大神们多多指教。
现在的Matlab程序,部分代码如下:
function choose_fathr_grid(cor_ci_gho500_may, %
cor_ci_gho500_june, %
cor_ci_gho500_july, %
gh_an_may_71_1_08_7, % 这几个是输入文件名
gh_an_jun_71_1_08_7, %
gh_an_jul_71_1_08_7, %
fa_thr_10m, %
an_num_cig_47yr_545st_drought_son,%-------------------------------------
fa_thr_obs) % 这个输出文件名
%clear all
yr=38;np_chn=545;lat1=73;lon1=144;nf=3;np=20;
fid1=fopen(cor_ci_gho500_may,'r');
Ro(:,:,1)=fread(fid1,[lat1*lon1 np_chn],'float32');
fid2=fopen(cor_ci_gho500_june,'r');
Ro(:,:,2)=fread(fid2,[lat1*lon1 np_chn],'float32');
fid3=fopen(cor_ci_gho500_july,'r');
Ro(:,:,3)=fread(fid3,[lat1*lon1 np_chn],'float32');
.......
如果我修改函数接口,传进来的数据是double型指针,那么该怎么修改读数据这部分,才能同现在的代码达到同样的目标呢?
代码如下:
// 调用Matlab导出库函数,利用通过显著性检验的相关系数,根据一定的间隔选取预报因子,并构造预报因子与预报量矩阵
libChooseFathrGridInitialize();
const mwArray mw_cor_ci_gho500_may((const char*)cor_ci_gho500_may);
const mwArray mw_cor_ci_gho500_june((const char*)cor_ci_gho500_june);
const mwArray mw_cor_ci_gho500_july((const char*)cor_ci_gho500_july);
const mwArray mw_gh_an_may((const char*)gh_an_may);
const mwArray mw_gh_an_june((const char*)gh_an_june);
const mwArray mw_gh_an_july((const char*)gh_an_july);
const mwArray mw_fa_thr_10m((const char*)fa_thr_10m);
const mwArray mw_an_num_cig_47yr_545st_drought_son((const char*)an_num_cig_47yr_545st_drought_son);
const mwArray mw_fa_thr_obs((const char*)fa_thr_obs);
choose_fathr_grid(mw_cor_ci_gho500_may, mw_cor_ci_gho500_june, mw_cor_ci_gho500_july,
mw_gh_an_may, mw_gh_an_june, mw_gh_an_july, mw_fa_thr_10m,
mw_an_num_cig_47yr_545st_drought_son, mw_fa_thr_obs);
libChooseFathrGridTerminate();
这个也能满足要求,但是在Matlab中来回读写数据感觉很慢,我想改成在C++里读写数据,将传给Matlab函数的参数改成double型的指针,请问诸位,我要怎么修改Matlab函数的接口,以及它内部的变量呢?对Matlab不是很熟悉,希望大神们多多指教。
现在的Matlab程序,部分代码如下:
function choose_fathr_grid(cor_ci_gho500_may, %
cor_ci_gho500_june, %
cor_ci_gho500_july, %
gh_an_may_71_1_08_7, % 这几个是输入文件名
gh_an_jun_71_1_08_7, %
gh_an_jul_71_1_08_7, %
fa_thr_10m, %
an_num_cig_47yr_545st_drought_son,%-------------------------------------
fa_thr_obs) % 这个输出文件名
%clear all
yr=38;np_chn=545;lat1=73;lon1=144;nf=3;np=20;
fid1=fopen(cor_ci_gho500_may,'r');
Ro(:,:,1)=fread(fid1,[lat1*lon1 np_chn],'float32');
fid2=fopen(cor_ci_gho500_june,'r');
Ro(:,:,2)=fread(fid2,[lat1*lon1 np_chn],'float32');
fid3=fopen(cor_ci_gho500_july,'r');
Ro(:,:,3)=fread(fid3,[lat1*lon1 np_chn],'float32');
.......
如果我修改函数接口,传进来的数据是double型指针,那么该怎么修改读数据这部分,才能同现在的代码达到同样的目标呢?