matlab与C++以.mat文件方式进行数据相互流动

简介:

年前,放假回家之前,使用了C++与matlab之间的数据的互动的一个实验,感觉效果挺好。初步达到了目的,所以整理下来方便大家使用。减少大家编程学习的时间。
希望对你们有用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "mat.h"<br data-filtered="filtered"><br data-filtered="filtered">void example8()
{
    const  char  **fnames;        /* pointers to field names */
     const  mwSize *dims;
     mxArray    *tmp, *fout;
     char        *pdata=NULL;
     int         i_field, nfields;
     mxClassID  *classIDflags;
     mwIndex    j_struct;
     mwSize     NumStructRecords;
     mwSize     ndim;
     
     MATFile *pMF;
     mxArray *pRoad, *pOut;
     int  status;
     const  char  *file =  "ContourRoadResults_AfterWidthEtcCorrect_4_ZY.mat" ;
     
 
     printf ( "Opening file %s...\n\n" , file);
     pMF = matOpen(file,  "u" );
     pRoad=matGetVariable(pMF,  "ContourRoadResults" );
    
     /* get input arguments */
     nfields = mxGetNumberOfFields(pRoad);
     NumStructRecords = mxGetNumberOfElements(pRoad);
     /* allocate memory  for storing classIDflags */
     classIDflags = (mxClassID*) mxCalloc(nfields,  sizeof (mxClassID));
     
     /* check empty field, proper data type, and data type consistency;
      * and get classID for each field. */
     for (i_field=0; i_field<nfields; i_field++)
     {
         for (j_struct = 0; j_struct < NumStructRecords; j_struct++)
         {
             tmp = mxGetFieldByNumber(pRoad, j_struct, i_field);
             if (tmp == NULL)
             {
                 printf ( "%s%d\t%s%d\n" "FIELD: " , i_field+1,  "STRUCT INDEX :" , j_struct+1);
                 //mexErrMsgIdAndTxt( "MATLAB:phonebook:fieldEmpty", "Above field is empty!");
             }
 
             if (j_struct==0)
             {
                 if ( (!mxIsChar(tmp) && !mxIsNumeric(tmp)) || mxIsSparse(tmp)) {
                     printf ( "%s%d\t%s%d\n" "FIELD: " , i_field+1,  "STRUCT INDEX :" , j_struct+1);
                    // mexErrMsgIdAndTxt( "MATLAB:phonebook:invalidField",
                    //         "Above field must have either string or numeric non-sparse data.");
                 }
                 classIDflags[i_field]=mxGetClassID(tmp);
             }
             else
             {
                 if  (mxGetClassID(tmp) != classIDflags[i_field])
                 {
                     printf ( "%s%d\t%s%d\n" "FIELD: " , i_field+1,  "STRUCT INDEX :" , j_struct+1);
                     //mexErrMsgIdAndTxt( "MATLAB:phonebook:invalidFieldType",
                     //        "Inconsistent data type in above field!");
                 else  if (!mxIsChar(tmp) &&
                         ((mxIsComplex(tmp) || mxGetNumberOfElements(tmp)!=1))){
                     printf ( "%s%d\t%s%d\n" "FIELD: " , i_field+1,  "STRUCT INDEX :" , j_struct+1);
                     //mexErrMsgIdAndTxt( "MATLAB:phonebook:fieldNotRealScalar",
                     //        "Numeric data in above field must be scalar and noncomplex!");
                 }
             }
         }
     }
     
     /* allocate memory  for storing pointers */
     fnames = ( const  char  **)mxCalloc(nfields,  sizeof (*fnames));
     /* get field name pointers */
     for  (i_field=0; i_field< nfields; i_field++){
         fnames[i_field] = ( char *)mxGetFieldNameByNumber(pRoad,i_field);
     }
 
     /* create a 1x1 struct matrix for output  */
     pOut = mxCreateStructMatrix(1,1,nfields, fnames);
     mxFree(( void  *)fnames);
     ndim = mxGetNumberOfDimensions(pRoad);
     dims = mxGetDimensions(pRoad);
     for (i_field=0; i_field<nfields; i_field++) {
         /* create cell/numeric array */
         if (classIDflags[i_field] == mxCHAR_CLASS) {
             fout = mxCreateCellArray(ndim, dims);
         } else  {
             fout = mxCreateNumericArray(ndim, dims, classIDflags[i_field], mxREAL);
             pdata = ( char *)mxGetData(fout);
         }
         /* copy data from input structure array */
         for  (j_struct=0; j_struct<NumStructRecords; j_struct++) {
             tmp = mxGetFieldByNumber(pRoad,j_struct,i_field);
             if ( mxIsChar(tmp)) {
                 mxSetCell(fout, j_struct, mxDuplicateArray(tmp));
             } else  {
                 mwSize     sizebuf;
                 sizebuf = mxGetElementSize(tmp);
                 memcpy (pdata, mxGetData(tmp), sizebuf);
                 pdata += sizebuf;
             }
         }
         /* set each field in output structure */
         mxSetFieldByNumber(pOut, 0, i_field, fout);
     }
 
     matPutVariable(pMF,  "OutputResult_Convert" ,pOut);
     mxFree(classIDflags);
     if  (matClose(pMF) != 0) {
         printf ( "Error closing file %s\n" ,file);
         return ;
     }
     printf ( "Done\n" );
     return ;
}

 项目需要进行事先的C++ MEX混合编程的标准配置,再此不再赘述。要提醒的是,需要加入的头文件是#include "mat.h"。另外,具体的其他函数,请参照matlab的 MAT文件的读写的相关内容。

这个帮助对相关函数说明比较详细,参照相关的demo文件,你就能很快上手。

祝愿能解决你的问题!

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。





   本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5223671.html,如需转载请自行联系原作者

相关文章
|
3月前
|
算法 计算机视觉 异构计算
基于FPGA的图像RGB转HSV实现,包含testbench和MATLAB辅助验证程序
基于FPGA的图像RGB转HSV实现,包含testbench和MATLAB辅助验证程序
|
4月前
|
算法 计算机视觉 异构计算
基于FPGA的图像差分运算及目标提取实现,包含testbench和MATLAB辅助验证程序
基于FPGA的图像差分运算及目标提取实现,包含testbench和MATLAB辅助验证程序
|
26天前
|
存储 算法 算法框架/工具
基于HSV色度空间的图像深度信息提取算法FPGA实现,包含testbench和MATLAB辅助验证程序
该文档介绍了在一个FPGA项目中使用HSV色彩模型提取图像深度信息的过程。通过将RGB图像转换为HSV,然后利用明度与深度的非线性映射估计深度。软件版本为Vivado 2019.2和MATLAB 2022a。算法在MATLAB中进行了对比测试,并在FPGA上实现了优化,包括流水线并行处理和查找表技术。提供的Verilog代码段展示了RGB到灰度的转换。实验结果和核心程序的图片未显示。
|
7月前
MATLAB实战 | 平面桁架结构受力分析
平面桁架结构受力分析
217 0
|
10月前
|
机器学习/深度学习 传感器 安全
基于Matlab模拟汇聚偏振光干涉
基于Matlab模拟汇聚偏振光干涉
|
11月前
|
机器学习/深度学习 传感器 算法
基于Matlab模拟5段S形多轴同步规划
基于Matlab模拟5段S形多轴同步规划
|
11月前
MATLAB连续LTI系统的时域分析(十)
MATLAB连续LTI系统的时域分析(十)
146 1
|
12月前
【Matlab】基于紧格式动态线性化的无模型自适应控制
【Matlab】基于紧格式动态线性化的无模型自适应控制
|
机器学习/深度学习 传感器 开发框架
基于Matlab实现自动频域分解(AFDD)
基于Matlab实现自动频域分解(AFDD)
|
机器学习/深度学习 传感器 算法
基于Matlab实现齿轮系统的传递路径分析 (TPA)
基于Matlab实现齿轮系统的传递路径分析 (TPA)

热门文章

最新文章