Ohio State nav bar

Run MATLAB Code on Any Operating System

October 1, 2019

Run MATLAB Code on Any Operating System

Ohio State seal

1.1 Directory Separators
Directory separators are the characters computers use to delimit path components. Most operating systems use “/” as the directory separator, but Windows operating systems use “\”. To maintain working scripts between different operating systems I wrote the function GETWACK. “Wack” is a slang term for a directory separator. GETWACK determines whether to use a forward or backward slash depending on the current operating system.

function wack = GETWACK() if ispc wack = "\"; else wack = "/"; end

end

1.2 Setting Directories

If you work on multiple computers, or have an advisor who looks at your code, it is useful to objectively orient the directories. To do so I use my function SETROOTDIR. ROOTDIR is an abbreviation of root directory, this function sets different paths based on the current user of the computer. Note the single use of GETWACK on lines 5-7, the other parts of the paths are hard coded for a Mac.

function [PCARootDir,ABSRootDir,ControlsRootDir,FigRootDir,SlopeTablesRootDir] = SETROOTDIR if getenv(’USER’) == "Parker" addpath(’/Users/Parker/Dropbox/Research-JPE/Scripting/m’); PCARootDir = ’/Users/Parker/Dropbox/Research-JPE/Scripting/PCARootDir’; ABSRootDir = [PCARootDir, GETWACK,’PPyCl09_PCA-APaseData/All_APase_Data’]; ControlsRootDir = [PCARootDir, GETWACK,’PPyCl09_PCA-APaseData/All_APase_Data/... ControlFiles’]; FigRootDir = ’[PCARootDir, GETWACK,’PPyCl09_PCA-APaseData/Figs’]; elseif getenv(’USER’) == "Vishnu" PCARootDir = ”; end end
 Screen shot of an early version of SETROOTDIR without GETWACK

Figure 1: Screen shot of an early version of SETROOTDIR without GETWACK

1.3 Creating Directories

If you run the previous example pointing to a directory that does not exist, you receive an error. However, if you perform the same operation in Python, the directory is created then set to the variable. Since I learned Python first, I wanted the default MATLAB behavior to create a directory if I referred to it by name before explicitly creating it. To do so I wrote the function CHECK_MAKE.

function Directory = CHECK_MAKE(folder) if exist(folder, ’dir’) Directory = folder; else Directory = mkdir(folder); end end

1.4 Tips

  1. MATLAB files cannot cannot have the same name as any functions within the file

  2. All of the functions I write are fully capitalized so that I can find them easily within my own scripts

  1. Do not include ’/Macintosh HD’ in your path name

  2. Mac users can right click on any file or folder, hold the option key down, and select Copy

...as Pathname to quickly copy a path
  1. [... , ...] is the shorthand syntax for horizontal concatenation in MATLAB

  2. getenv is an abbreviation of get environmental variable; environmental variables persist be- yond MATLAB and are used by many different programs

1.5 References

1. Shure, L. What’s in Your Startup.m. Loren on the Art of MATLAB (2009). Available at: https://blogs.mathworks.com/loren/2009/03/03/whats-in-your-startupm.

Written by TPS Fellow Parker Evans

News Filters: