{"id":31,"date":"2016-07-23T13:28:42","date_gmt":"2016-07-23T21:28:42","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=31"},"modified":"2016-11-27T13:14:32","modified_gmt":"2016-11-27T21:14:32","slug":"structuring-your-matlab-code-base","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2016\/07\/23\/structuring-your-matlab-code-base\/","title":{"rendered":"Structuring your MATLAB code base"},"content":{"rendered":"<p>When your MATLAB project gets large, or when you have a lot of projects, it&#8217;s time to consider\u00a0restructuring your code and libraries so\u00a0you can focus on what matters the most instead of plowing through\u00a0the mess you just created.<\/p>\n<p>For my projects, I usually create a file\u00a0called &#8216;managedPathAndFiles_{myProjectName}.m&#8217; at the top-level\u00a0folder. The comments in the demo code below highlight the techniques used:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"matlab\">function [file, folder] = managedPathAndFile_myProject(isRegenerated)\r\n% isRegenerated: set to 'false' to skip addpath() (which is slow)\r\n\r\n % Optional default input arguments by name instead of couting nargin\r\n if( ~exist('isRegenerated', 'var') )\r\n   isRegenerated = true;\r\n end\r\n\r\n % Note the use of nested structures (like replacing _ with .)\r\n % You can use the hierarchy to group folders\/files in a way you can \r\n % operate on them in one-shot\r\n\r\n % Use the location of this file as anchor \r\n % 'pwd' is unreliable because you can call this from other folders if\r\n % it's in MATLAB's path\r\n folder.root = fileparts( mfilename('fullpath') );\r\n \r\n % Include your project specific subroutines in the MATLAB path\r\n % Use fullfile() to generate platform independent folder structures\r\n folder.core.root = fullfile(folder.root, 'core');\r\n folder.core.helper = fullfile(folder.core.root, 'helper'); \r\n % Add all the paths under the folder in one shot\r\n if( isRegenerated )\r\n   % '-end' avoids name conflict by yielding to the incumbent paths \r\n   addpath( genpath(folder.core.root), '-end' );\r\n end\r\n \r\n % Automatically create data storage folder\r\n folder.data.root = fullfile(folder.root, 'data');\r\n folder.data.cache = fullfile(folder.data.cache, 'data');\r\n if( isRegenerated )\r\n   % Outputting something will absorb the error if the path alreayd\r\n   % exist. I made a mkdir_silent() in my libraries, but used the\r\n   % native call here for demo.\r\n   [~, ~] = structfun(@mkdir, folder.data);\r\n end\r\n \r\n % Sometimes you might have config or external files to load\r\n file.data.statistics = fullfile(folder.data.root, 'statistics.mat');\r\n \r\nend<\/pre>\n<p>Many people don&#8217;t know about the function genpath() so they ended up lumping all their dependencies in one folder\u00a0which makes my eyes bleed. Organize your files into a tree that makes sense now!<\/p>\n<p>I&#8217;d recommend\u00a0any serious MATLAB developer build their own library folder with a consistent naming and a sensible tree hierarchy. After looking into FEX and what&#8217;s natively available in MATLAB and you still need to\u00a0roll out your own code, you&#8217;re likely to rediscover what you&#8217;ve already built just by establishing a new .m file\/function you are about to write in the folder you&#8217;d most naturally put it in (like people with like mind: self, tend to pick the same names).<\/p>\n<p>Sometimes you have to whip up some &#8216;crappy&#8217; code that doesn&#8217;t generalize (or can be reused) in other contexts. Consider putting them in a \/private folder under the caller so it won&#8217;t be visible to everybody else. Of course, I encourage people spend a little more time to write the code properly so it can be put in your own MATLAB library.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_31\" class=\"pvc_stats all  \" data-element-id=\"31\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When your MATLAB project gets large, or when you have a lot of projects, it&#8217;s time to consider\u00a0restructuring your code and libraries so\u00a0you can focus on what matters the most instead of plowing through\u00a0the mess you just created. For my &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2016\/07\/23\/structuring-your-matlab-code-base\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_31\" class=\"pvc_stats all  \" data-element-id=\"31\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-matlab"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":7,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions\/340"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}