-2

I have codes which are written in C++ and I would like to use them in Matlab. In my folder I have these files:

  • do_it.cpp
  • hist (I guess it's a binary file)
  • hist_lebesgue.cpp
  • hist_lebesgue.hpp
  • tools.cpp
  • tools.hpp

The program is supposed to make a pdf distribution of your data set.

I was told that to run the code in Linux (I guess by C++ compiler) you need to call the binary 'hist' from the linux commandline. If you are in the folder where the binary lies then you need to type for example

['./hist 1000 2 path_to_file'].

path_to_file is the path of your data file.

Now I want to use this code to run it in Matlab. Could someone please help me how I can do this. What should I type in Matlab's command Window?

PS. I am using mac.

Mina
  • 11
  • 7
  • You tell us that you are not using Linux. Since matlab runs on windows, Linux and OSX please tell us what you ARE using as this influences the required steps you need to take. – Kris Aug 19 '14 at 19:24

1 Answers1

0

Assuming you use Windows OS X: You can't run a Linux executable on Windows OS X. You have two options:

  • Compile the program for Windows OS X with the MinGW OS X version of gcc / g++.

  • If that doesn't work, or you find it too difficult: Install Linux (e.g. Debian) in a virtual machine (e.g. VirtualBox), and install the Linux version of Matlab in it.

EDIT: Once you have installed the compiler on your system (I trust you can find tutorials for that with Google), you could try this command for compiling the program:

g++ hist_lebesgue.cpp tools.cpp do_it.cpp -o hist

I can't promise it will work though, since I don't know the source code and can only guess how to compile it. You should ask the person who gave you the source code for instructions how to compile it.

chris
  • 2,541
  • 1
  • 23
  • 40
  • I have mac. Would that help? – Mina Aug 19 '14 at 19:33
  • Not really - the same advice is still valid, except that you have to replace "MinGW version of gcc" with "OS X version of gcc" – chris Aug 19 '14 at 19:36
  • Sorry, for asking again but I'm very naive in working with C++ and Matlab. Could you please tell me how I should exactly compile the program. I guess first I have to run mex -setup. Then I don't know what I should do. My main function is "do_it.cpp" which starts like this: #include "hist_lebesgue.hpp" #include "tools.hpp" int main(int argc, char *argv[]), so what should I exactly run? and how? – Mina Aug 19 '14 at 19:49
  • See edit. I don't think you need MEX, because if I understand you correctly, you are just calling an executable, not a function in a library. If you really do need MEX, I can't really help you, because it has been ages I have ever used that and would have to look it up myself. – chris Aug 19 '14 at 19:59
  • Thanks for your help. I tried it but it didn't work. – Mina Aug 19 '14 at 20:01
  • Well, I think your best bet would be to ask the person who gave you the source code for help. But in case it's just a simple problem, if you post the compiler error message, there is a small chance I can help you with it. – chris Aug 19 '14 at 20:04
  • First I wanted to check if I have g++ installed on my mac, so I run mex -setup and I get this message o override the default options file, use the 'mex -f' command (see 'mex -help' for more information). The options files available for mex are: 1: /Applications/MATLAB_R2013a.app/bin/mexopts.sh : Template Options file for building MEX-files 0: Exit with no changes Enter the number of the compiler (0-1): and I choose 1 – Mina Aug 20 '14 at 09:22
  • Then it asks: Overwrite /Users/minas/.matlab/R2013a/mexopts.sh ([y]/n)? and I answer y – Mina Aug 20 '14 at 09:23
  • And it gives me this message : /Applications/MATLAB_R2013a.app/bin/mexopts.sh is being copied to /Users/minas/.matlab/R2013a/mexopts.sh And now, being in the same folder that my files are, I run the command that you mentioned, i.e., g++ hist_lebesgue.cpp tools.cpp do_it.cpp -o hist(1000,2,data) – Mina Aug 20 '14 at 09:26
  • Then I get this error: g++ hist_lebesgue.cpp tools.cpp do_it.cpp -o hist(1000,2,data) | Error: Unexpected MATLAB expression. – Mina Aug 20 '14 at 09:26
  • Ahhhh ... you shouldn't run that command in Matlab, but in the Terminal. g++ is not a Matlab command, it's a standalone command line program that has nothing to do with Matlab. – chris Aug 20 '14 at 09:43
  • You _really_ should read up on the basics of how to use a C/C++ compiler before trying to compile anything yourself. – chris Aug 20 '14 at 09:45
  • ok from terminal I am now in my folder and I ran g++ hist_lebesgue.cpp tools.cpp do_it.cpp -o hist, then I get this message: In file included from hist_lebesgue.cpp:1: ./hist_lebesgue.hpp:11:10: fatal error: 'interface/gnuplot_i.h' file not found #include "interface/gnuplot_i.h" ^ 1 error generated. In file included from do_it.cpp:4: ./hist_lebesgue.hpp:11:10: fatal error: 'interface/gnuplot_i.h' file not found #include "interface/gnuplot_i.h" ^ 1 error generated. – Mina Aug 20 '14 at 10:14
  • Then obviously the program needs gnuplot - an external library - to compile. I won't write a tutorial how to handle external dependencies, that goes way beyond the scope of this question. You should read up on that topic and ask a new question if you need help with compiling. But trust me, it will take a lot of time to learn the basics - to be honest, you should go the Virtual Machine route that I suggested. – chris Aug 20 '14 at 10:44