# Best practice for compiling MATLAB code

**URL:** <https://ask.cyberinfrastructure.org/t/best-practice-for-compiling-matlab-code/2852>\
**Category:** Discussion Zone\
**Tags:** matlab, programming-for-hpc\
**Created:** [June 14, 2023, 8:22pm UTC](https://ask.cyberinfrastructure.org/t/best-practice-for-compiling-matlab-code/2852 "2023-06-14T20:22:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitchellxh](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/mitchellxh/32/1033_2.png) [@mitchellxh](https://ask.cyberinfrastructure.org/u/mitchellxh)\
**Post date:** [June 14, 2023, 8:22pm UTC](https://ask.cyberinfrastructure.org/t/best-practice-for-compiling-matlab-code/2852/1 "2023-06-14T20:22:23Z")

</div>

It’s been some time since I’ve needed to use MATLAB non-interactively and am wondering what’s the best practice for compiling my code and submitting as batch jobs. Additionally, my center’s HPC has a limited number of shared MATLAB licenses, so ideally I would be setting up a MATLAB runtime environment to avoid abusing my “share”.

Is the [deploytool](https://www.mathworks.com/help/compiler/deploytool.html) still a recommended method? Are there any good tools, walkthroughs, steps I should follow?

Thanks!

---

<div class="post-metadata">

**Author:** ![sbhopala](https://ask.cyberinfrastructure.org/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@sbhopala](https://ask.cyberinfrastructure.org/u/sbhopala)\
**Post date:** [June 15, 2023, 7:55pm UTC](https://ask.cyberinfrastructure.org/t/best-practice-for-compiling-matlab-code/2852/2 "2023-06-15T19:55:05Z")

</div>

I have never used deploytool. But for submitting MATLAB batch jobs, I have used job scripts like this on the cluster.

```auto
# !/bin/bash
#BSUB -n 1
#BSUB -W 01:00

bsub -R "matlab -nodisplay -singleCompThread -r <name_of_matlabscript>"

```

you may tune this script for a different job scheduler like SLURM or PBS. More details are available in this link ([https://scicomp.ethz.ch/wiki/MATLAB](https://scicomp.ethz.ch/wiki/MATLAB)). I do not have experience in compiling MATLAB programs, but the link provided above contains some information on compilation in the cluster.

---

<div class="post-metadata">

**Author:** ![pagondas](https://ask.cyberinfrastructure.org/letter_avatar_proxy/v4/letter/p/bcef8e/32.png) [@pagondas](https://ask.cyberinfrastructure.org/u/pagondas)\
**Post date:** [June 17, 2023, 1:01pm UTC](https://ask.cyberinfrastructure.org/t/best-practice-for-compiling-matlab-code/2852/3 "2023-06-17T13:01:49Z")

</div>

The latest versions of Matlab can now be invoked noninteractively with the flag “-batch”. This is recommended over “-r” (or “\<“) with “-nodisplay” because it:

- Starts without the desktop
- Does not display the splash screen
- Disables changes to preferences
- Disables toolbox caching
- Logs text to stdout and stderr
- Does not display modal dialog boxes
- Exits automatically with exit code 0 if script executes successfully. Otherwise, MATLAB terminates with a non-zero exit code.

Example running program1.m:  
matlab -batch “program1”
