MATLAB System Command Gives Segmentation Fault? - matlab

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

Related

Want to activate a virtual environment from terminal with Octave/Matlab

I would like to execute a bash command to activate a virtual environment with Octave using Linux. What I actually want to do is run DeepSpeech using Octave/Matlab.
The command I want to use is
source $HOME/tmp/deepspeech-venv/bin/activate
The line of code I tried on my own is system("source $HOME/tmp/deepspeech-venv/bin/activate")
And the output I'm getting is sh: 1: source: not found
I saw this answer on a post and tried this command setenv('PATH', ['/source $HOME/tmp/deepspeech-venv/bin/activate', pathsep, getenv('PATH')]) but with no help it returned the same error.
It's not completely clear from your question, but I'm assuming you're trying to do is run python commands within octave/matlab, and you'd like to use a python virtual environment for that.
Unfortunately, when you run a system command from within octave, what most likely happens is that this creates a subshell to execute your command, which is discarded once the command has finished.
You have several options to rectify this, but I think the easiest one would be to activate the python virtual environment first, and run your octave instance from within that environment. This then inherits all environmental variables as they existed when octave was run. You can confirm this by doing getenv( 'VIRTUAL_ENV' ).
If that's not an option, then you could make sure that all system commands intended to run python scripts, are prefixed with a call to the virtual environment first (e.g. something like system( 'source ./my/venv/activate; python3 ./myscript.py') ).
Alternatively, you can try to recreate the virtual environment from its exported variables manually, using the setenv command.

Running Matlab via PBS fails

I am trying to run Matlab 2013 via PBS and I get the following error:
libXv.so.1: cannot open shared object file: No such file or directory
I can run my code in Matlab's GUI mode though. I tried using LD_PRELOAD to point to the library as under, but that did not help either.
setenv LD_PRELOAD /usr/lib/x86_64-linux-gnu/libXv.so.1
Any suggestions on how to go about resolving this error?
When running Matlab via PBS you run actual Matlab scripts on the nodes of the cluster that probably do not have any GUI components installed including the X client libraries such as libXv.so.1.
You probably do not want to run Matlab in a GUI mode via a batch system unless it is an interactive job. In case you just want to run a script, try specifying matlab command line in your PBS script as follows:
matlab -nodisplay -nodesktop -nojvm -nosplash -r "myfunc"
Where myfunc is the Matlab command that you want to run on the cluster.

Running external program in Matlab fails but program works outside it

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

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.

Run a MATLAB script from Windows DOS prompt

I am trying to run a Matlab script from Windows command prompt but I can't execute it sometimes. The script runs fine when manually launched. Matlab version is 2011a and Windows is Server 2003 SP2. Details:
Script mytask.m is located inside say E:\Production\Project. This is SAVED on Matlab's path.
When I place mytask.m inside bin folder, it executes fine by the command:
`C:\Program Files\MATLAB\R2011a\bin>matlab -r mytask`
If you delete it and try to access it at its original location, the script doesn't run although Matlab editor window is launched:
`C:\Program Files\MATLAB\R2011a\bin>matlab -r "E:\Production\Project\mytask"
Any suggestions please? Thanks.
The syntax for matlab -r is
matlab -r "statement"
In other words, you need to provide some executable commands as the statement. For example:
matlab -r "run E:\Production\Project\mytask"
However, it seems that matlab does not load the customized paths in this way. If you have some customized paths, you probably have to define them in startup.m and place this startup.m in the directory where you invoke matlab.
I didn't check myself, but if you define E:\Production\Project\ as the path in startup.m, you probably can run matlab -r mytask without problem, as mytask will be recognized as a user function/script.
A simple example of startup.m
path(path, 'E:\Production\Project\');