You are not logged in.

#1 2011-02-04 15:57:02

mikesol
Member
From: Toulouse, France
Registered: 2011-02-01
Posts: 14

Learning how to use C++ with Matlab files

Hi guys!

I'm Ph.D  student who needs to run thousands of mathematical simulations. Actually I'm working in MatLab, but due the massive quantity of simulations, I decided to make some tests in C++ in order to improve the time of the computations.

Well the thing is that all my data it's in mat files (Matlab filetype) and i tried three things for access them:

  1. Use the library <fstream> but i found extremely complicated, specially with double numbers. (In fact i have to convert all my data to a text raw file)

  2. Use the native API of C++ from Matlab, but i get the error

    Warning: You are using gcc version "4.5.2".  The version
             currently supported with MEX is "4.3.4".
             For a list of currently supported compilers see: 
             http://www.mathworks.com/support/compilers/current_release/

    and until now i don't know how to resolve it.

  3. Finally i found one library  http://sourceforge.net/projects/matio/  which in theory can handle mat files in C++ but I haven't could make it work in this test example http://na-wiki.csc.kth.se/mediawiki/index.php/MatIO:

    #include <matio.h>
    
    int main(void)
    {
    
      const double a[5] = {43.5, 5432.434, 3.32, 0.0001, 88834.0};
    
      mat_t *mat;
      matvar_t *matvar;
      int dims[2] = {1,5};
    
      mat = Mat_Open("simple.mat",MAT_ACC_RDWR);
    
      if(mat)
        {
          matvar = Mat_VarCreate("vec1",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,a,0);
          Mat_VarWrite( mat, matvar, 0);
    
          Mat_VarFree(matvar);
          Mat_Close(mat);
        }
      else
        return 1;
        
      return 0;
    }

    because i get this:

    /home/mikesol/projects/test/build> make
    Scanning dependencies of target test
    [100%] Building CXX object CMakeFiles/test.dir/main.cpp.o
    /home/mikesol/projects/test/main.cpp: In function ‘int main()’:
    /home/mikesol/projects/test/main.cpp:16:73: error: invalid conversion from ‘const void*’ to ‘void*’
    /home/mikesol/projects/test/main.cpp:16:73: error:   initializing argument 6 of ‘matvar_t* Mat_VarCreate(const char*, int, int, int, int*, void*, int)’
    make[2]: *** [CMakeFiles/test.dir/main.cpp.o] Error 1
    make[1]: *** [CMakeFiles/test.dir/all] Error 2
    make: *** [all] Error 2
    *** Failed ***

    i'm not so good in C++ to improve the code or know what is wrong.  I'm sorry for that.


Summarizing: I'm stuck, so any help will be welcome.

Thanks in advance.


--
Sorry for my English.

Offline

#2 2011-02-04 17:08:54

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Learning how to use C++ with Matlab files

The bug is that "a" is defined as a "const double[]", but Mat_VarCreate() apparently wants a non-const pointer.  So make it a "double[]" instead.

Offline

#3 2011-02-04 17:17:18

jowilkin
Member
Registered: 2009-05-07
Posts: 243

Re: Learning how to use C++ with Matlab files

mikesol wrote:
Warning: You are using gcc version "4.5.2".  The version
         currently supported with MEX is "4.3.4".
         For a list of currently supported compilers see: 
         http://www.mathworks.com/support/compilers/current_release/

To solve this one, you could just install an older version of gcc on your machine.  gcc 4.3.4 is available in the AUR, the package name is gcc43.

Offline

#4 2011-02-04 17:24:49

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,358

Re: Learning how to use C++ with Matlab files

I remove matlab's built-in gcc, it will then use system gcc. For writing mex files the examples in Matlab's help is plenty. Search for the error, I've posted solutions for it before.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#5 2011-02-05 14:20:29

mikesol
Member
From: Toulouse, France
Registered: 2011-02-01
Posts: 14

Re: Learning how to use C++ with Matlab files

thanks to all,

@ngoonee: I'm going to try the solution described in your post https://bbs.archlinux.org/viewtopic.php?id=86809.

@tavianator: Thanks for the solution, but now there are some "undefined reference" in the compilation

/home/mikesol/projects/test/build> make
[100%] Building CXX object CMakeFiles/test.dir/main.cpp.o
Linking CXX executable test
CMakeFiles/test.dir/main.cpp.o: In function `main':
/home/mikesol/projects/test/main.cpp:14: undefined reference to `Mat_Open'
/home/mikesol/projects/test/main.cpp:18: undefined reference to `Mat_VarCreate'
/home/mikesol/projects/test/main.cpp:19: undefined reference to `Mat_VarWrite'
/home/mikesol/projects/test/main.cpp:21: undefined reference to `Mat_VarFree'
/home/mikesol/projects/test/main.cpp:22: undefined reference to `Mat_Close'
collect2: ld returned 1 exit status
make[2]: *** [test] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***

I will return later to work in that. Thanks to all.


--
Sorry for my English.

Offline

#6 2011-02-05 16:12:09

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,358

Re: Learning how to use C++ with Matlab files

Yeah, it took me a couple of hours to figure that out from the (almost useless) matlab 'forums'.

But since then had no problems. Been using the same method over the past 3 years of my PhD studies.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

Board footer

Powered by FluxBB