{"id":175,"date":"2017-04-19T15:39:55","date_gmt":"2017-04-19T19:39:55","guid":{"rendered":"https:\/\/carleton.ca\/rcs\/?page_id=175"},"modified":"2026-04-09T13:59:07","modified_gmt":"2026-04-09T17:59:07","slug":"matlab-tips","status":"publish","type":"page","link":"https:\/\/carleton.ca\/rcs\/rcdc\/matlab-tips\/","title":{"rendered":"Matlab on Linux Servers"},"content":{"rendered":"\n<section class=\"w-screen px-6 cu-section cu-section--white ml-offset-center md:px-8 lg:px-14\">\n    <div class=\"space-y-6 cu-max-w-child-5xl  md:space-y-10 cu-prose-first-last\">\n\n            <div class=\"cu-textmedia flex flex-col lg:flex-row mx-auto gap-6 md:gap-10 my-6 md:my-12 first:mt-0 max-w-5xl\">\n        <div class=\"justify-start cu-textmedia-content cu-prose-first-last\" style=\"flex: 0 0 100%;\">\n            <header class=\"font-light prose-xl cu-pageheader md:prose-2xl cu-component-updated cu-prose-first-last\">\n                                    <h1 class=\"cu-prose-first-last font-semibold !mt-2 mb-4 md:mb-6 relative after:absolute after:h-px after:bottom-0 after:bg-cu-red after:left-px text-3xl md:text-4xl lg:text-5xl lg:leading-[3.5rem] pb-5 after:w-10 text-cu-black-700 not-prose\">\n                        Matlab on Linux Servers\n                    <\/h1>\n                \n                                \n                            <\/header>\n\n                    <\/div>\n\n            <\/div>\n\n    <\/div>\n<\/section>\n\n\n\n<h2 id=\"using-parallelism-in-matlab\" class=\"wp-block-heading\">Using Parallelism in Matlab<\/h2>\n\n\n\n<p>One way to improve MATLAB code performance is using parallelism.\u00a0 In MATLAB, the most common way to do this is to replace a slow\u00a0<strong class=\"myprefix-text-bold\">for<\/strong><strong>\u00a0<\/strong>loop with a\u00a0<strong class=\"myprefix-text-bold\">parfor<\/strong> loop.<\/p>\n\n\n\n<p>However, there are some restrictions on what loops can be converted to\u00a0<strong class=\"myprefix-text-bold\">parfor<\/strong>.\u00a0 Loops must be independent.\u00a0 That is, the loop iterations should be executable in any order, where the results of one iteration do not rely on previous iterations.<\/p>\n\n\n\n<p>More information on converting loops to <strong class=\"myprefix-text-bold\">parfor <\/strong>can be found at:\u00a0\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/distcomp\/troubleshoot-variables-in-parfor-loops.html\">https:\/\/www.mathworks.com\/help\/distcomp\/troubleshoot-variables-in-parfor-loops.html<\/a><\/p>\n\n\n\n<h2 id=\"using-the-console\" class=\"wp-block-heading\">Using the Console<\/h2>\n\n\n\n<p>It is possible to use MATLAB without its graphical interface.&nbsp; In particular, on remote Linux servers it is often best to run MATLAB from the command-line and not interactively.<\/p>\n\n\n\n<p>Once your MATLAB code is finished and tested, you can run it from the Linux server&#8217;s shell:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>SSH to remote server<\/li>\n\n\n\n<li>Change directory to your MATLAB script&#8217;s location\u00a0<em>(<\/em><em class=\"myprefix-text-italic\">let&#8217;s assume your script is called myexample.m)<\/em><\/li>\n\n\n\n<li>Run this command: <\/li>\n<\/ol>\n\n\n<div class=\"not-prose cu-quote cu-component-spacing\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>matlab -nodisplay -r &#8216;myexample; quit&#8217;<\/p>\n<\/blockquote>\n<\/div>\n\n\n<p>You can even put this MATLAB command in the background using techniques found at either:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"s1\"><a href=\"https:\/\/carleton.ca\/rcs\/linux-basics\/\">https:\/\/carleton.ca\/rcs\/linux-basics\/<\/a><\/span><\/li>\n\n\n\n<li><span class=\"s1\"><a href=\"https:\/\/carleton.ca\/rcs\/background-terminal-sessions\/\">https:\/\/carleton.ca\/rcs\/background-terminal-sessions\/<\/a>.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 id=\"forcing-single-threaded-execution\" class=\"wp-block-heading\">Forcing Single-Threaded Execution<\/h2>\n\n\n\n<p>MATLAB has commands that operate on entire matrices or vectors at once.&nbsp; These are called vectorized operations.&nbsp; MATLAB automatically runs many vectorized operations in parallel.<\/p>\n\n\n\n<p>Sometimes you don&#8217;t want MATLAB to automatically use multiple processor cores.\u00a0 Perhaps you want to evaluate how effective MATLAB&#8217;s core use is, or perhaps you need to share the cores with other programs.\u00a0 You can disable MATLAB&#8217;s automatic parallelism by invoking matlab from the command line with this option: <\/p>\n\n\n<div class=\"not-prose cu-quote cu-component-spacing\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>matlab\u00a0-singleCompThread<\/p>\n<\/blockquote>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Using Parallelism in Matlab One way to improve MATLAB code performance is using parallelism.\u00a0 In MATLAB, the most common way to do this is to replace a slow\u00a0for\u00a0loop with a\u00a0parfor loop. However, there are some restrictions on what loops can be converted to\u00a0parfor.\u00a0 Loops must be independent.\u00a0 That is, the loop iterations should be executable [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":96,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_cu_dining_location_slug":"","footnotes":"","_links_to":"","_links_to_target":""},"cu_page_type":[],"class_list":["post-175","page","type-page","status-publish","hentry"],"acf":{"cu_post_thumbnail":""},"_links":{"self":[{"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/pages\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":3,"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/pages\/175\/revisions"}],"predecessor-version":[{"id":1173,"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/pages\/175\/revisions\/1173"}],"up":[{"embeddable":true,"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/pages\/96"}],"wp:attachment":[{"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"cu_page_type","embeddable":true,"href":"https:\/\/carleton.ca\/rcs\/wp-json\/wp\/v2\/cu_page_type?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}