将.mat文件中的值输入到C++中的数组中
Inputting values from .mat file to array in C++
我正试图将一个1100x1100矩阵从.mat文件复制到C++中类型为float的数组变量中。我在网上阅读,发现matio库是一个不错的选择。我在Ubuntu 12.04上使用"make"安装了他们的库(我遵循了他们网页上给出的方法)。然而,我无法使用它编写代码,主要是因为我是C++的新手。我正在用g++编译这个文件。我会遇到诸如"未知引用Mat_Open"之类的错误。我确实在网页上找到了这段代码:
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY); //here argv[1] is "a.mat"?
if ( NULL == matfp )
{
fprintf(stderr,"Error opening MAT file %s0,argv[1]);
return EXIT_FAILURE;
}
matvar = Mat_VarReadInfo(matfp,"x"); // x is the variable we are trying to access?
if ( NULL == matvar )
{
fprintf(stderr,"Variable ’x’ not found, or error reading MAT filen");
}
我有几个问题:
- 这里,argv[1]对应于我试图打开的.mat文件,对吗
- 此代码中的x是我试图复制的.mat文件中的变量吗
- 当我运行这段代码时,我收到了一个错误,上面写着-对Mat_Open的未知引用等等
我使用g++ abc.cpp -o test
编译了这个。(后面是./test
。但由于编译过程中出现的错误,我一直没有抽出时间)。
我怎样才能让它工作?我使用的代码有什么错误吗?或者,对于我正在使用的compile语句,可能有一些链接需要用于编译。
谢谢。请记住,我是C++的新手。任何建议都会有帮助。
1)argv[1]-是您在程序调用后放入的第一个参数。如果你想"感觉",可以使用调试器或类似的代码:
#include <iostream>
for (unsigned i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
2) 是的,看着http://libmatio.sourcearchive.com/documentation/1.3.4/group__MAT_g4c8205ff25c5b688a40775fbb1840b7e.html我可以说,您将读取名为"x"的变量。
3) "未定义引用"意味着您需要使用matio库进行构建。在编译字符串中添加类似"-lLibraryName"的内容。它将不得不建造。
为了避免许多问题,请尝试安装Code::Blocks,它是跨平台的,如果你以前从未使用过C++,那么很容易开始使用它。它还支持调试器,因此可以很容易地避免许多问题。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '