AS-74.
3176 Software for Control Design
Using MATLAB with COMSOL Multiphysics
Often COMSOL models need to be exported to the MATLAB workspace or a co-operating SIMU-
LINK simulator for scripting usage. Also sometimes MATLAB m-scripts are a handy way to add func-
tionality to your COMSOL model. In both of these cases the information transfer between COMSOL
and MATLAB can be done by using them both together in a connected manner.
Connecting
The simplest and recommended way to use MATLAB and COMSOL together is by, in the very begin-
ning, selecting “Start COMSOL Multiphysics with MATLAB” in the COMSOL start menu. This will
start both MATLAB and COMSOL and configure the necessary interconnection settings. The MAT-
LAB working directory still needs to be set manually, however.
If you’re already running COMSOL, the best is to “connect COMSOL with MATLAB” from the
COMSOL graphical user interface. This is done as follows:
1) From the “File” menu choose “Client/Server/MATLAB” and “Connect to MATLAB”. This
will open a dialogue asking whether you want to start MATLAB and connect COMSOL with
the MATLAB server. Press “OK”.
2) Once MATLAB opens, set the MATLAB working directory to the directory where your
COMSOL model resides.
Notions and troubleshooting
1) The firewall installed on the computer may possibly warn when COMSOL is creating the
connection to the MATLAB server. Accept the connection, if a warning occurs. Also, if no
connection between the programs can be established, check your firewall settings for connec-
tion blocks.
2) The MATLAB root folder may change over updates and if COMSOL can not find the MAT-
LAB root, a connection between the two can not be made. The MATLAB root folder can be
configured for COMSOL in the comsol.opts file found in the COMSOLROOT/bin folder.
Edit the value of the “mlroot” variable. COMSOL needs to be restarted after editing the file.
3) If you need to use your own scripts also in MATLAB, you may need to alter the MATLAB
search path variable (“help path”). Typically, adding own scripts to the MATLAB search
path is done with “addpath(‘c:\ownpath\...’)”
Warning
Closing MATLAB without disconnecting from COMSOL first (“File” -> “Client/Server/MATLAB”
and “Disconnect from MATLAB”) will lead to write block of COMSOL and essentially stop all model
editing, including saving the model.