Running external program in Matlab fails but program works outside it - matlab

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.

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

Related

Matlab Unknown class error after compiling script containing TCPIP

I compiled a script that uses the tcpip function from the instruments toolbox using MCR_R2015a. Running the generated executable on the PC I used for compiling (Windows7) is not a problem, however running it on another PC (Windows10, no Matlab installed) is. I receive the following error:
unknown variable com or unknown class com.mathworks.toolbox.instrument.TCPIP
pointing to line 123 in tcpip. The surrounding code is:
try
obj.jobject = handle(com.mathworks.toolbox.instrument.TCPIP(host,port));
catch aException
newExc = MException('instrument:tcpip:cannotCreate',aException.message);
throw(newExc); %%% this is line 123
end
Obviously com.mathworks.toolbox.instrument.TCPIP is not known. I tried to find the file defining this object to append it when compiling without success.
Where to find that file or how to compile a running executable?
As I was able to run the script inside Matlab but not the executable using the Matlab compiler runtime, I figured an unset path in the MCR may cause the error.
Not being able to find the file were the path to the TCPIP class is defined, I ended up deinstalling the MCR. Et voilá, it worked!
I am aware this solution is not answering the question how to compile scripts containing the tcpip function and running the generated exe on PCs that have no Matlab installed. But it is good enough for me.

MATLAB System Command Gives Segmentation Fault?

I am using an external solver to run simulations. I am trying to call the external solver with system(cmd) from a MATLAB script. The cmd itself runs fine with no problems from the Terminal (I'm running on OSX). However, every time the MATLAB script runs, the status is 139 - Segmentation Fault. Does anyone know why this might be happening?
When executing the system command this is not the same as running a program from the terminal since you likely have environment variables defined within your terminal session (via ~/.bashrc or ~/.bash_profile) that are going to effect how a program is accessed and run.
If you have environment variables that you need to set for the program to run successfully, use setenv from within MATLAB prior to calling system.
I had the same problem with my Matlab (R2016b), but the solution was exactly the opposite.
Instead of missing variables, system() defined LD_LIBRARY_PATH which redirected to some shared libraries packaged with Matlab that didn't sit well with my program. After clearing LD_LIBRARY_PATH in my script, everything worked fine. You can do that for example with this:
env -u LD_LIBRARY_PATH

Execute batch file using dos()

I got a problem when executing batch file commands through matlab. This batch file includes commands to run simulations in Adams. When I execute the batch file directly from DOS window, it works well. But if I use matlab to execute it (using command dos()), it gives error saying 'cannot check out the license for Adams'.
This confuses me: if the license is incorrect, it should not work no matter I execute the batch file directly in DOS or ask MATLAB to execute it. I also tried to execute other DOS commands through matlab using dos() and it worked well.
Does anyone know what the problem may be?
Such issues are commonly caused by some environment variables being changed or cleared by MATLAB. I have very similar experience on Linux and Mac OS X, where this causes havoc when using system or unix.
In Unix-like systems, MATLAB is started from a shell script where all of this happens. So you can either incorporate missing variables there or in the .matlab7rc.sh in your home directory (the latter is preserved when you upgrade MATLAB and it is much easier to use). I won't go into all the Unix details here.
An alternative workaround is to explicitly set those variables when you issue a system command (e.g. system('export variable=value ; ...')). It is quite a bit of work, but you can then use that MATLAB code on different computers with ease.
On Windows, I'm not completely sure about the exact location of the corresponding files (and whether MATLAB starts in quite a similar way as on Unix). But if they exist, you can probably find it in the MATLAB documentation.
Anyhow, the alternative fix should work here as well.
First you need to diagnose which system variables you need (likely a PATH or anything that has a name related to Adams).
To do so in Windows, run set from the Windows command prompt (cmd.exe) and from within MATLAB. Whatever differs in the output is a possible suspect for your problem.
To inspect just a single variable, you can use the command echo %variablename%.
I will assume that you have found that the suspect environment variable is missing and should be set to value.
The workaround fix is then to run your command in MATLAB as system('set suspect=value & ...') where you replace ... with your original command.

Running a C++ program from an m.file

For a project I am working on I'm preparing data in MATLAB, then running the data through a separate external application (written in C++) named Model v2.exe, then performing further analysis with the output in MATLAB. I'm trying to create an M-file which does all of this, but I am struggling to get the C++ program to launch from my MATLAB code.
How can I launch an external application from within my MATLAB code?
You can either make use of the ! operator, or the system() command.
First, rename you application to something that has no spaces in the name, like modelv2.exe. Next, either make sure it is in the system path, as defined by your system environment variables, or generate a full path to it (eg: C:\Users\Phil\Desktop\modelv2.exe).
You can call an executable program from the command line using the exclamation point or the system command:
!modelv2
or:
!C:\Users\Phil\Desktop\modelv2.exe
will cause Windows to execute the program hello.exe if there is such a file in the current directory or on the system path. Alternatively:
system('modelv2');
or
system('C:\Users\Phil\Desktop\modelv2.exe');
will do the same thing.
References
"MATLAB Central - call and run an external program in matlab under windows", Accessed 2014-03-19, <http://www.mathworks.com/matlabcentral/answers/11568-call-and-run-an-external-program-in-matlab-under-windows>

New to Matlab - Not sure how to run these files

The files I would like to run are found here:
http://www.mathworks.co.uk/matlabcentral/fileexchange/12552-multicanonical-monte-carlo-scheme-for-finding-rare-growth-factors/content/mcmc2.m
it consists of 3 files, one of which is called a driver.
I have tried running each individual one in Matlab and each one gets an error, I think this is because I need to run them all simultaneously or something?
At the beginning of the driver script it says "This is the driver script to be run from the command line. Also requires functions mcmc1 and mcmc2". Where mcmc1 and mcmc2 are the other two files I tried running it in command line but I kept getting error messages..
Any idea how I get these files to run?
You should just be able to run gf_mmc_driver from the command line in Matlab. The mcmc1.m and mcmc2.m files need to be in your matlab path, but do not need to be called directly.
However, I do notice in the driver file the following comment:
% Telling the Distributed Computing Toolbox to complete one job with
% 'numberchain' tasks. Each task is comprised of running a MCMC
% for the burnin time (mcmc1.m) with a different initial matrix.
Do you have the Distributed Computing Toolbox? The driver file appears to require it.