5

I need to use a toolbox in Matlab that depends on a external program written in C and using OpenCV. I'm using Ubuntu 14.04 64 bits and Matlab R2014b.

The m-script calls it with the system function and after the C program runs, it reads some output files that the C program should generate. This is failing to happen. I observed that the files are not being generated.

Then, the first thing I did was verify if the C program was working properly, by directly calling it in the terminal, with the same parameters that the m-script has used to call it. Then it worked and the files where generated. Thus, the C program is correct and working.

Coming back to Matlab, it failed again. Then, I started debugging the C program and had find out that the OpenCV function cvLoadImage is failing to open the file, when called from Matlab. It works if I call it outside.

I verified the string that is passed to the function both running from Matlab and externally and it is rigorously the same, but the Matlab call fails ever. I verified with ldconfig -p | grep opencv from the Matlab command window and it shows the libraries. More amusing, there are other calls to other ocv functions before the problematic one that are working. Just this is failing.

The question is: is this a problem of some misconfiguration of my part or maybe it is a bug in Matlab?

Thanks in advance.

EDIT

Actually, calling from inside Matlab with sudo appended to the system call got the job done. What is strange, because I used chmod 777 -Rf as last resource in the whole folder and verified that my user was the owner of both the folder and Matlab processes. Strange... It stills bothers me having to type my password whenever I use the script though, but my schedule is tight, I'll consider this workaround as satisfactory by now.

WDiniz
  • 115
  • 2
  • 7
  • 1
    are you using absolute path for the image file name or relative path? what is the working directory (`cwd`/`pwd`) when you run in Matlab and what is the `cwd` when you ran the program from shell? – Shai Feb 18 '15 at 14:00
  • @Shai Exactly the same, verified with pwd command in both terminals. When debugging, tested either passing relative and absolute path of the image, even hardcoding it. Always the same result, works outside, doesn't work inside. – WDiniz Feb 18 '15 at 14:09
  • `cvLoadImage` has poor to no error handling capabilities. Try opening the file with `fopen()`. If the open fails, it will set `errno`, which you can turn into an actual error string using `strerror`. You need the actual error message to debug this. – Peter Feb 18 '15 at 14:24
  • @Peter Thanks, fopen works with both... – WDiniz Feb 18 '15 at 15:17
  • 2
    The sudo note makes me wonder if its something to do with environment variables. (SUDO will launch a new shell). – Ashish Uthama Feb 18 '15 at 17:57

1 Answers1

0

Instead of calling the C function from MATLAB, you can try to call MATLAB from your C code with MATLAB Engine API for C: https://es.mathworks.com/help/matlab/calling-matlab-engine-from-c-programs-1.html

Sergio
  • 61
  • 3