{"id":103,"date":"2020-01-22T19:39:04","date_gmt":"2020-01-22T19:39:04","guid":{"rendered":"https:\/\/ni.cmu.edu\/computing\/?post_type=ht_kb&#038;p=103"},"modified":"2026-07-13T19:18:13","modified_gmt":"2026-07-13T19:18:13","slug":"matlab","status":"publish","type":"ht_kb","link":"https:\/\/ni.cmu.edu\/computing\/knowledge-base\/matlab\/","title":{"rendered":"Matlab"},"content":{"rendered":"<h1>Matlab<\/h1>\n<p><a href=\"https:\/\/www.mathworks.com\/\">Matlab<\/a> is a numerical computing and programming environment with a broad range of functionality (matrix manipulation, numerical linear algebra, general-purpose graphics, etc.). Additionally, special application areas are served by a large number of optional toolboxes.<\/p>\n<p>CMU&#8217;s Campus-Wide License covers Matlab, so both a local desktop installation and use on the MIND Cluster are available to you.<\/p>\n<p>To see the Matlab version currently installed on the cluster:<\/p>\n<pre><code>module avail matlab\r\n<\/code><\/pre>\n<p>As of this update, the cluster provides <strong>matlab_23.2<\/strong>. Load it with:<\/p>\n<pre><code>module load matlab_23.2\r\n<\/code><\/pre>\n<h4>Running Matlab on the headnode<\/h4>\n<p>Users need to avoid using the head node for processing using Matlab or other software. All Matlab work \u2014 interactive or batch \u2014 should run on a compute node via SLURM, not directly on the login\/head node.<\/p>\n<hr \/>\n<h2>Interactive Matlab: Two Options<\/h2>\n<p>There are two supported ways to work with Matlab interactively on the cluster: <strong>VS Code Remote-SSH<\/strong> (recommended) and <strong>X11 forwarding<\/strong> (traditional, still supported).<\/p>\n<h3>Option 1: VS Code Remote-SSH (recommended)<\/h3>\n<p>For the full setup (installing VS Code, configuring the required SSH <code>ProxyJump<\/code>, requesting a compute node, and connecting), see <a href=\"https:\/\/ni.cmu.edu\/computing\/knowledge-base\/ssh-vscode-setup\/\">SSH and VSCode Setup<\/a>.<\/p>\n<p>Once you&#8217;re connected to a compute node in VS Code, open a terminal and load Matlab as usual:<\/p>\n<pre><code>module load matlab_23.2\r\nmatlab\r\n<\/code><\/pre>\n<p>This gives you a full graphical Matlab session running on the cluster&#8217;s compute hardware, displayed locally through VS Code.<\/p>\n<h3>Option 2: X11 Forwarding<\/h3>\n<p>X Window System (X11) forwarding is still supported for users who prefer a traditional remote-display graphical session, without installing VS Code.<\/p>\n<p>You&#8217;ll need an X11 server on your local computer; how you get this depends on your local operating system. See <a href=\"https:\/\/ni.cmu.edu\/computing\/knowledge-base\/logging-onto-the-cluster\/\">Logging onto the Cluster<\/a> for setup details.<\/p>\n<p>Request an interactive session with X11 enabled:<\/p>\n<pre><code>srun --x11 -p cpu --cpus-per-task=1 --mem=10GB --time=4:00:00 --pty $SHELL\r\n<\/code><\/pre>\n<p>Adjust the partition, CPU count, memory, and time to match what your session needs.<\/p>\n<p>Example session:<\/p>\n<pre><code>$ ssh -Y &lt;your-andrew-id&gt;@mind.cs.cmu.edu\r\n[&lt;you&gt;@mind ~]$ srun --x11 -p cpu --cpus-per-task=1 --mem=10GB --time=4:00:00 --pty $SHELL\r\n[&lt;you&gt;@mind-0-15 ~]$ module load matlab_23.2\r\n[&lt;you&gt;@mind-0-15 ~]$ matlab\r\n<\/code><\/pre>\n<p>If you see:<\/p>\n<pre><code>srun: error: No DISPLAY variable set, cannot setup x11 forwarding.\r\n<\/code><\/pre>\n<p>make sure you&#8217;re connecting with <code>-Y<\/code> (enables trusted X11 forwarding), and that your local X11 server is running.<\/p>\n<hr \/>\n<h2>Non-Interactive (Batch) Matlab Jobs<\/h2>\n<p>For most production work, running Matlab non-interactively via a SLURM batch script is the preferred approach \u2014 it makes full use of cluster resources without tying up an interactive session.<\/p>\n<p>Matlab can be run non-interactively using input\/output redirection: <code>matlab &lt; myScript.m &gt; myOutput.txt<\/code>. Your main script should end with the <code>exit<\/code> command so Matlab quits automatically once finished.<\/p>\n<p><strong>Example:<\/strong> a script <code>mystats.m<\/code> containing a main function and two local helper functions:<\/p>\n<pre><code class=\"language-matlab\">function [avg, med] = mystats(x)\r\nn = length(x);\r\navg = mymean(x,n);\r\nmed = mymedian(x,n);\r\nend\r\n\r\nfunction a = mymean(v,n)\r\n% MYMEAN Example of a local function.\r\na = sum(v)\/n;\r\nend\r\n\r\nfunction m = mymedian(v,n)\r\n% MYMEDIAN Another example of a local function.\r\nw = sort(v);\r\nif rem(n,2) == 1\r\n    m = w((n + 1)\/2);\r\nelse\r\n    m = (w(n\/2) + w(n\/2 + 1))\/2;\r\nend\r\nend\r\n<\/code><\/pre>\n<p><strong>SLURM batch script<\/strong> (<code>run.sh<\/code>), submitted with <code>sbatch run.sh<\/code>:<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash -l\r\n#SBATCH --job-name=batch_matlab_example\r\n#SBATCH --ntasks=1\r\n#SBATCH -p cpu\r\n#SBATCH --mem=10gb\r\n#SBATCH --time=00-00:05:00\r\n#SBATCH --output=\/user_data\/&lt;your-andrew-id&gt;\/exampleOut.out\r\n\r\nhostname\r\necho \"job starting\"\r\nmodule load matlab_23.2\r\ncd \/path\/to\/your\/script\r\necho \"RUNNING MATLAB\"\r\nmatlab -nodisplay -nosplash &lt; mystats.m &gt; run.log\r\nmodule unload matlab_23.2\r\necho \"job finished\"\r\n<\/code><\/pre>\n<p>The <code>-nodisplay<\/code> flag runs Matlab without the GUI; <code>-nosplash<\/code> suppresses the startup logo. The <code>&lt;<\/code> operator feeds in your script; <code>&gt;<\/code> redirects output to a log file.<\/p>\n<p><strong>Note:<\/strong> don&#8217;t disable Java when launching Matlab (i.e. don&#8217;t use <code>matlab -nojvm<\/code>) \u2014 <code>matlabpool<\/code>\/parallel functionality depends on the Java Virtual Machine.<\/p>\n<hr \/>\n<h2>Running Matlab on Parallel Hardware<\/h2>\n<p>Matlab can take advantage of parallel\/multi-core hardware in a few different ways, covered below. See MathWorks&#8217; <a href=\"https:\/\/www.mathworks.com\/help\/parallel-computing\/\">Parallel Computing Toolbox documentation<\/a> for full details.<\/p>\n<h4>Exploiting trivial parallelism<\/h4>\n<p>An easy way to exploit multi-core systems is to split your workflow into independent parts \u2014 the classic example being a parameter sweep, where the same script runs many times with different inputs. Since these runs don&#8217;t depend on each other, they can be scheduled as a batch of independent jobs. This approach pairs well with Matlab&#8217;s <code>mcc<\/code> compiler, which helps avoid excessive license checkouts when running many instances at once.<\/p>\n<h4>Multi-threaded MEX programming<\/h4>\n<p>Mex (Matlab EXecutable) files are compiled subroutines (C, C++, or Fortran) that run from within Matlab like built-in functions. Combining Mex with <a href=\"https:\/\/en.wikipedia.org\/wiki\/OpenMP\">OpenMP<\/a> multi-threading is a powerful way to accelerate performance-critical sections of a Matlab program.<\/p>\n<p>Writing Mex files is most worthwhile when profiling identifies a specific, frequently-called bottleneck function. Operations that already rely on highly optimized libraries like FFTW (e.g. <code>fftn<\/code>) or BLAS\/LAPACK (e.g. <code>A\\b<\/code>) generally have little to gain from a Mex rewrite. See the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/345725-mex-programming-for-a-beginner\">MathWorks Mex programming guide<\/a> for an introduction.<\/p>\n<hr \/>\n<h2>Connecting a Local Matlab Desktop to the Cluster (Parallel Server)<\/h2>\n<p class=\"font-claude-response-body break-words whitespace-normal\" data-sourcepos=\"151:1-151:429;5963-6391\">Matlab&#8217;s Parallel Computing Toolbox supports submitting jobs to a remote cluster directly from a local Matlab Desktop installation, without logging into the cluster at all. <strong>This is not yet a supported, self-serve workflow on the MIND Cluster.<\/strong> We haven&#8217;t validated the SLURM submission plugin or off-campus network behavior for this configuration, so we can&#8217;t currently offer setup support or guarantee it will work reliably.<\/p>\n<p class=\"font-claude-response-body break-words whitespace-normal\" data-sourcepos=\"153:1-153:264;6393-6656\"><strong>For interactive, GUI-based Matlab work, use <a class=\"underline underline underline-offset-2 decoration-1 decoration-current\/40 hover:decoration-current focus:decoration-current\" href=\"https:\/\/ni.cmu.edu\/computing\/knowledge-base\/ssh-vscode-setup\/\">VS Code Remote-SSH<\/a> instead<\/strong> \u2014 it gives you a responsive local editing experience backed by the cluster&#8217;s compute power, and it&#8217;s fully supported today.<\/p>\n<p class=\"font-claude-response-body break-words whitespace-normal\" data-sourcepos=\"155:1-155:235;6658-6892\">If you have a strong need for direct desktop-to-cluster job submission, <a class=\"underline underline underline-offset-2 decoration-1 decoration-current\/40 hover:decoration-current focus:decoration-current\" href=\"https:\/\/ni.cmu.edu\/computing\/contact-it-support\/\">contact NI Support<\/a> to discuss \u2014 we may be able to pilot this with you, but please expect some trial and error.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matlab Matlab is a numerical computing and programming environment with a broad range of functionality (matrix manipulation, numerical linear algebra, general-purpose graphics, etc.). Additionally, special application areas are served by a large number of optional toolboxes. CMU&#8217;s Campus-Wide License covers Matlab, so both a local desktop installation and use on&#8230;<\/p>\n","protected":false},"author":1,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[11],"ht-kb-tag":[],"class_list":["post-103","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-software"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/ht-kb\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":10,"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/ht-kb\/103\/revisions"}],"predecessor-version":[{"id":742,"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/ht-kb\/103\/revisions\/742"}],"wp:attachment":[{"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/ht-kb-category?post=103"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/ni.cmu.edu\/computing\/wp-json\/wp\/v2\/ht-kb-tag?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}