Matlab mex: More elegant way to setup env.sh?

The way we need to generate library paths with env.sh seem really clunky - basically, I either need to use the folder
{acados}/examples/acados_matlab_octave/my_example
to store my own examples, or modify this file by hand…

And if I’m doing that, woudln’t there be a better solution, once I know where my acados is? Thus far I’ve simply hard-coded the path to acados folder, but still run the env.sh every time before opening matlab…

Can we instead place this somewhere in bash and make it run once our system starts? What would be the downsides of that? (sorry, I’m not an expert in Linux)

Hello Anton,
You are totally free to add acados related to your ~/.bashrc or equivalent,
the env.sh was meant to work in the general case without requiring users to mess with their shell configuration files.
For more involved users the natural next step is to add lines or includes in ~/.bashrc
I would add something like:

export ACADOS_INSTALL_DIR=<your/acados/path/(the one cotaining lib/ and include/)> 
export ACADOS_SOURCE_DIR=<your/acados_source/path> 
# casadi and mex path
export MATLABPATH=$MATLABPATH:$ACADOS_SOURCE_DIR/external/casadi-matlab/
export MATLABPATH=$MATLABPATH:$ACADOS_SOURCE_DIR/interfaces/acados_matlab_octave/
export MATLABPATH=$MATLABPATH:$ACADOS_SOURCE_DIR/interfaces/acados_matlab_octave/acados_template_mex/
# acados shared library
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ACADOS_INSTALL_DIR/lib
# ocp description and non-linear functions libraries
MODELS=('<path/to/ocp1' '<path/to/ocp2>' '...' ) # assuming ocp descriptions are not necessarily in the acados/examples folder 
for MODEL in "${MODELS[@]}"; do export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MODEL/build; done

I agree this is not so “elegant”, but it the only thing we are aware of which can work with MATLAB,
if you know how to set those environment variables (in order to be accessible to the linker ) from MATLAB please let us know

1 Like

Apparently, it’s been like that with Matlab for at least 5 years, good to know that there’s at least one constant in this crazy world :rofl:

1 Like

Hi,

There is the option to edit Matlab’s rc-script. That is .matlab7rc.sh located in /usr/local/MATLAB/R20XXY/bin on my Ubuntu 18.04 machine.

At the top of the file, I mimiced the env.sh script by adding the lines:

export ENV_RUN=true

export ACADOS_INSTALL_DIR="/directory/of/my/acados/installation"

export MATLABPATH=$MATLABPATH:$ACADOS_INSTALL_DIR/external/casadi-matlab/
export MATLABPATH=$MATLABPATH:$ACADOS_INSTALL_DIR/interfaces/acados_matlab_octave/
export MATLABPATH=$MATLABPATH:$ACADOS_INSTALL_DIR/interfaces/acados_matlab_octave/acados_template_mex/

# probably useless
export OCTAVE_PATH=$OCTAVE_PATH:$ACADOS_INSTALL_DIR/external/casadi-octave/
export OCTAVE_PATH=$OCTAVE_PATH:$ACADOS_INSTALL_DIR/interfaces/acados_matlab_octave/
export OCTAVE_PATH=$OCTAVE_PATH:$ACADOS_INSTALL_DIR/interfaces/acados_matlab_octave/acados_template_mex/

MODEL_FOLDER=${MODEL_FOLDER:-"./build"}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ACADOS_INSTALL_DIR/lib:$MODEL_FOLDER
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ACADOS_INSTALL_DIR/interfaces/acados_template/tera_renderer/t_renderer/target/release

This script runs before MATLAB links to the libraries specified in LD_LIBRARY_PATH. So when I launch MATLAB now, the environment variables are set as required by acados. :slight_smile:

Maybe you could add some procedure to acados’ installation that perform above steps automatically / adds the lines to MATLAB’s .matlab7rc.sh script. Then the procedure of sourcing the env.sh is not required to be performed by the user.

Andi

2 Likes

Thanks @andis, this looks like a good suggestion! :open_hands:
I made an issue to not forget about it.