将.mat文件中的值输入到C++中的数组中

Inputting values from .mat file to array in C++

本文关键字:C++ 数组 输入 mat 文件      更新时间:2023-10-16

我正试图将一个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++,那么很容易开始使用它。它还支持调试器,因此可以很容易地避免许多问题。