Get user home directory in Matlab on Windows

The tilde “~” character is used by most terminal shells as shorthand for the user home directory. The home directory is typically a safer place to write files than the system root directory, so ~ give a convenient way to refer to an absolute path generic across systems. Many code languages require parsing the tilde, including Python, C++, Fortran and Matlab. GNU Octave understands that ~ tilde is the user’s home directory on any operating system, even Windows. Matlab does not consistently understand ~ as the user home directory, particularly on Windows.

To fix this issue, we created expanduser.m that works on Linux, Mac and Windows.


returns the absolute path of the user home directory, for example:

  • Linux: /home/username
  • MacOS: /Users/username
  • Windows: C:\Users\Username