c/c++中读取和写入mat文件 --- MAT File I/O Library(二)

本篇记述c/c++中读取mat文件示例

测试数据matioTest.zip下载地址:

http://pan.baidu.com/s/1sjPkMsd

1. 读取matioTest.zip中s.mat文件,数据为普通matrix

c/c++中读取和写入mat文件 --- MAT File I/O Library(二)
 1 #include <iostream>
 2 #include <cassert>
 3 #include <matio.h>
 4 
 5 
 6 int main()
 7 {
 8     mat_t *matfp;
 9     const char* matFilePath = "testRes\\s.mat";
10     matfp = Mat_Open(matFilePath, MAT_ACC_RDONLY);
11     if (NULL == matfp) {
12         fprintf(stderr, "Error opening MAT file %s\n", matFilePath);
13         return EXIT_FAILURE;
14     }
15     matvar_t* matVar = NULL;
16     // read data ‘x‘
17     matVar = Mat_VarRead(matfp, "x");
18     if (matVar) {
19         assert(matVar->data_type == MAT_T_DOUBLE);
20         size_t xSize = matVar->nbytes / matVar->data_size;
21         const double* xData = static_cast<const double*>(matVar->data);
22         for (int i = 0; i < xSize; ++i) {
23             std::cout << xData[i] << " ";
24         }
25         std::cout << std::endl;
26         for (int i = 0; i < matVar->rank; ++i) {
27             std::cout << matVar->dims[i] << " ";
28         }
29         std::cout << std::endl;
30     }
31     // read data ‘y‘
32     matVar = Mat_VarRead(matfp, "y");
33     if (matVar) {
34         assert(matVar->data_type == MAT_T_DOUBLE);
35         size_t ySize = matVar->nbytes / matVar->data_size;
36         const double* yData = static_cast<const double*>(matVar->data);
37         for (int i = 0; i < ySize; ++i) {
38             std::cout << yData[i] << " ";
39         }
40         std::cout << std::endl;
41         for (int i = 0; i < matVar->rank; ++i) {
42             std::cout << matVar->dims[i] << " ";
43         }
44         std::cout << std::endl;
45     }
46     // read ‘minX‘ and ‘step‘ is similar
47     Mat_VarFree(matVar);
48     Mat_Close(matfp);
49     system("pause");
50     return EXIT_SUCCESS;
51 }
View Code

 

 

2. 读取matioTest.zip中image_0001.mat文件,数据为struct

c/c++中读取和写入mat文件 --- MAT File I/O Library(二)
 1 #include <iostream>
 2 #include <cassert>
 3 #include <matio.h>
 4 
 5 int main()
 6 {
 7     mat_t *matfp;
 8     const char* matFilePath = "testRes\\image_0001.mat";
 9     matfp = Mat_Open(matFilePath, MAT_ACC_RDONLY);
10     if (NULL == matfp) {
11         fprintf(stderr, "Error opening MAT file %s\n", matFilePath);
12         return EXIT_FAILURE;
13     }
14     matvar_t *feaSetStruct = NULL, *feaArr = NULL;
15     feaSetStruct = Mat_VarRead(matfp, "feaSet");
16     if (feaSetStruct) {
17         assert(MAT_T_STRUCT == feaSetStruct->data_type);
18         feaArr = Mat_VarGetStructFieldByName(feaSetStruct, "feaArr", 0);
19         if (feaArr) {
20             assert(MAT_T_DOUBLE == feaArr->data_type);
21             size_t Size = feaArr->nbytes / feaArr->data_size;
22             const double* Data = static_cast<const double*>(feaArr->data);
23             // print Data or copy to other memory container
24         }
25     }
26     Mat_VarFree(feaSetStruct);
27     system("pause");
28     return EXIT_SUCCESS;
29 }
View Code

 

 

 参考资料:

http://sourceforge.net/p/matio/discussion/609377/thread/b703ce7a/

http://sourceforge.net/p/matio/discussion/609376/thread/9df1158c/?limit=50#1cac/10aa

c/c++中读取和写入mat文件 --- MAT File I/O Library(二),布布扣,bubuko.com

c/c++中读取和写入mat文件 --- MAT File I/O Library(二)

上一篇:c语言全局变量和局部变量问题汇总


下一篇:SpringMVC重定向视图RedirectView小分析