필터 지우기
필터 지우기

Problem extracting data out of a variable of mat file in C++

조회 수: 12 (최근 30일)
Qandeel
Qandeel 2012년 6월 26일
I have to extract the data out of a variable(double array) of a mat file for use in my C++ code.I have tried the mxGetPr, mxGetVariable and mxGetData functions but they do not seem to work.I am able to read the mat file and even get the variable's name and no. of rows and columns but I haven't been able to extract data.Please help me out ,need urgent help!!
  댓글 수: 2
James Tursa
James Tursa 2012년 6월 26일
Please post you code so we can point out the errors and suggest corrections.
Qandeel
Qandeel 2012년 6월 27일
편집: Walter Roberson 2012년 6월 28일
Here is my code -I have actually modified the matlab diagnose mat file code and added the getvariable command to extract the data.
#include <stdio.h>
#include <stdlib.h>
#include "mat.h"
#include "matrix.h"
#include "mex.h"
#include <iostream>
int diagnose(const char *file) {
MATFile *pmat;
const char **dir;
const char *a;
int ndir;
int i;
mxArray *pa;
double *pr;
printf("Reading file %s...\n\n", file);
/*
* Open file to get directory
*/
pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", file);
return(1);
}
/*
* get directory of MAT-file
*/
dir = (const char **)matGetDir(pmat, &ndir);
if (dir == NULL) {
printf("Error reading directory of file %s\n", file);
return(1);
} else {
printf("Directory of %s:\n", file);
for (i=0; i < ndir; i++)
printf("%s\n",dir[i]);
}
mxFree(dir);
/* Read in each array. */
printf("\nReading in the actual array contents:\n");
/*Get Variable*/
pa = matGetVariable(pmat, file);
//I donot know how to get the variable pointed to by pointer 'pa'
and use it in my C++ code.
int main(int argc, char **argv)
{
int result;
if (argc > 1)
result = diagnose(argv[1]);
else{
result = 0;
printf("Usage: matdgns <matfile>");
printf(" where <matfile> is the name of the MAT-file");
printf(" to be diagnosed\n");
}
return (result==0)?EXIT_SUCCESS:EXIT_FAILURE;
}

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2012년 6월 28일
Your incorrect code:
/*Get Variable*/
pa = matGetVariable(pmat, file);
The 2nd argument is supposed to be the variable name, not the file name. E.g.,
/*Get Variable*/
pa = matGetVariable(pmat, dir[i]);
Then you can get at the data using mxGetPr(pa) etc. Just be sure not to free dir before you are done with it. And when you are done with it, free each dir[i] first since all of these strings are dynamically allocated also.
  댓글 수: 9
Ketan
Ketan 2012년 7월 1일
The %d format specifier indicates an integer argument, but I do not think the compiler converts the double *pr to int.
Try casting *pr from a double to an int datatype. For example:
...%d\n",(int)(*pr));
You only see one element because you are not iterating over the #elements in the array (the variable l).
This stack overflow article goes into some detail regarding the printf issue:
Qandeel
Qandeel 2012년 7월 1일
Thank you Ketan for your timely help, the casting solved my issue. I am really grateful to your help on my question .

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by