I have two questions (and answers to them). I could make two separate questions and answers of them, but I won't, because the second question is the same as the first, but with a twist. The two parts of the answer I post below solve both the question's parts. So I chose to keep this a single Q&A.

Part 1

Changing the default MATLAB layout

The default MATLAB layout (on first start up) is IMHO quite user unfriendly. It might be ok for beginners, but superusers (like you and me) don't want to permanently see some of the MATLAB window's (called the desktop) elements. This is what MATLAB R2014a (and the newer versions as well, mostly) looks like by default:

enter image description here

Let's see what's wrong with it, IMHO:

  1. The window isn't positioned nicely: it doesn't use the full screen, neither does it use a practical fraction (e.g. the left, upper, bottom or right half) of the screen. Let's fix that.
  2. The ribbon (the upper part in the MATLAB window containing the Home, Plots and Apps tabs) is expanded, which I don't need most of the time. So I want to collapse it to save some screen estate.
  3. The Current Folder pane to the left is expanded, which I don't need most of the time, but if expanded it's there most of the time. So most of the time it uses precious screen estate, like the ribbon.
  4. The right pane, the Workspace, is very useful if you do calculations directly in the Command Window (middle pane) and you need to keep track of what your variables are, etc. However, while scripting or writing functions using the Editor (not visible in the default window). You don't need the Workspace that much, because your variables can be quite abstract. While debugging the Workspace might be handy, though, but I usually collapse it to save some more screen estate. Instead of the Workspace, I use the whos command in the Command Window to list all my variables. Pro tip: to list all variables that start with e.g. an ‘x’, use whos x*.
  5. The ‘New to MATLAB? ...’ tip at the top of the Command Window annoys me, because I'm absolutely not new to MATLAB. More yellow tips like these exist when you open some other panes or windows.
  6. The current folder (different for everybody's default MATLAB) is not a practical folder. I want it to default to the folder I actually store my work in.

There's probably more stuff you or I don't like or want to change, but let's fix these six issues, see Part 1 of my answer below for my solution.

Part 2

Making the custom settings persist on computers that reset them

An additional problem I encountered with the above problems was not solved by Part 1 of my answer. The problem, I imagine, happens to more people that use MATLAB on a computer of which they are not an administrator, i.e. they possibly do not have elevated permissions. This means they cannot edit system settings and possibly cannot edit MATLAB's setting either, as happens in my case. I use MATLAB on a work computer at a very large workplace. Every computer has a virtual workspace that users log in to. Most drives are virtual: MATLAB's installation directory is on one of those drives. Through Windows Explorer I can't even see the drive! Additionaly, the shortcut to MATLAB points to a different program my workplace uses to call any software the user is licensed to use, not directly to MATLAB.exe in the MATLAB installation folder. However, a MATLAB command exists that reveals MATLAB's root folder, matlabroot.

The problem I had after setting my MATLAB desktop the way I liked (see the screenshot in my answer below), was that the layout reverted to the unfriendly default layout after every reboot. I reboot at least once per day, so this means my MATLAB settings and layout reset every time. This might not be a problem for you, but for a superuser that uses some custom settings, layouts, etc. this is a problem. I have solved it in Part 2 of my answer, see below.


Part 1

Changing the default MATLAB layout

We solve the six listed issues by changing the desktop layout to whatever we like better. I use a layout that looks like this by default:

Default MATLAB window on first start up

Let's reproduce that.

  1. Simply drag the main MATLAB window to where you want it, e.g. maximise it. Solved!
  2. Click the arrow icon right beneath the red exit × in the top right corner of the MATLAB window to collapse the ribbon. Solved!
  3. Drag the title of the Current Folder pane (the part where it says Current Folder) to wherever you want it. You can put it almost anywhere. While dragging, you see the pane's shadow of where it will be positioned when you release the mouse button. You can position the pane elsewhere, or you can drag it to the top left, left top, right top, top, bottom, or any of the other pane's titles to create tabbed Current Folder pane that is closed by default. MATLAB calls this minimised. When a pane is minimised, it takes up much less space and can be opened by either clicking on the tab or hovering your mouse over the tab. Another solution is to completely close the pane, removing it from you MATLAB desktop. This can be done by first undocking the pane by right clicking the title → click on Undock (or pressing Ctrl+Shift+U). This opens the pane in a separate window. Close the window and it will be gone. If you accidentally lose a window like this, you can restore it through the Home tab of the main ribbon → click on Layout → enable Current Folder again. To dock the window again, click on the tiny arrow in the top right corner of the pane's window → click on Dock (or pressing Ctrl+Shift+D). I put the Current Folder in a tab on the top left.
  4. I put the Workspace next to the Current Folder tab. Solved!
  5. Simply click on the × to the right in the yellow tip's bar.
  6. Choose a folder you want to default to and browse to it using either the Current Folder pane or the address bar (in the screenshot above, it's the dropdown list right below the ribbon that says ► C: ►). To set this as the default path, copy the address first. I believe that in MATLAB versions since R2014b you can set the default current folder in MATLAB's preferences (find it through the ribbon Home tab → Preferences → General preferences → Initial working folder). If you're on an older version, then you can set the default MATLAB current folder using the userpath(newpath) command, in which newpath is the path you copied. Note that newpath should be a string, i.e. enclosed by 'single straight apostrophes', so not the "double apostrophes" you might have copied. Solved!

The Command History (accessed by pressing in the Command Window) and the Editor (press Ctrl+N in the Command Window to open it) were repositioned as well.

It is a good idea to save this layout, so you can easily switch to it if something is changed by something or somebody (e.g. when multiple users use the same MATLAB with different layouts). Do this trough the ribbon Home tab → Layout → Save Layout → give your layout a name. It is saved as an .xml file called <your layout name>MATLABLayout.xml in the MATLAB preferences directory, which you can find with the prefdir command. All of MATLAB's preferences, command histroy, default layout, etc. are stored in this directory. This is very useful knowledge for the second part of this answer!

Most (if not all) other preferences are stored there as well.

Now, the second part of my answer, which answers the Part 2 of my question.

Part 2

Making the custom settings persist on computers that reset them

Before everything else: Apply all your custom setting and save a layout for your desired MATLAB desktop as described in Part 1 of this answer.

Now, we're going to cleverly use startup.m, finish.m and a few .bat files.

startup.m is a file that, if present on the MATLAB path, runs at startup before you gain control over the MATLAB window. This file is a script that can be used to do anything. We will set it in a way that will do the following:

  1. Set the Current Folder to whatever directory we want after startup.
  2. Overwrite the default MATLAB preferences with our own, as saved in Part 1 of this answer.
  3. Restore our own MATLAB desktop layout.

The first two steps are the easiest. In my case (on my work computer(s)), the default path simply was forgotten everytime the computer was restarted. So I made a script startup.m and I put the following code in it.

(this goes in startup.m)

%% set path
p = 'C:\Whatever\path\you\want\as\your\default\Current Folder';

Easy enough. Replace p with whatever path to a current folder you want.

The second step is more difficult. It requires both saving and restoring all settings in MATLAB's preference directory, which can be found using the prefdir command. For this, we use finish.m as well. This is a script that automatically runs just before MATLAB exits. MATLAB can be exited in a few ways: click the × of the main MATLAB window, or use the exit or quit command. Good to know: If you kill MATLAB.exe through the Windows Task Manager or through a different system command that kills the process, finish.m will not run. Make sure finish.m is on the MATLAB path or you add the path to it to the MATLAB path using addpath in the code snippet above for startup.m. Otherwise, finish.m won't run when MATLAB quits.

We will make finish.m copy all the preferences you might have set or changed and your custom layout(s) to a folder that you have permissions to. The following code does this.

(this goes in finish.m)

%% backup MATLAB preferences, etc., see also: startup.m
p = 'path\to\your\preference backup folder';
copyfile([prefdir filesep '*'], p, 'f')

This copies all files in the MATLAB preference directory to your backup folder of choice. We now tell startup.m to restore these files. Because startup.m runs before anything else, it nicely restores all settings we might want. The following code does this for us.

(this goes in startup.m)

%% restore MATLAB preferences, etc., see also: finish.m
p = 'path\to\your\preference backup folder'; % end without a file separator, i.e. '/', '\' or filesep
copyfile([p filesep '*'], prefdir, 'f')

In this code snippet p must be set to the same directory as p in finish.m, so MATLAB knows where to copy the file from.

The third item on our list is the most complicated one. Simply copying the <your layout name>MATLABLayout.xml file using the previous code snippet to MATLAB's preference directory isn't enough to restore the layout. You need to restore it programmatically. Furthermore, in my situation MATLAB required a restart before the layout was applied. This will be done with the following steps.

I found a method on Undocumented MATLAB, a website that describes many of MATLAB's functions that aren't documented or supported by MATLAB's developers. They are not easy to use and can behave strangely, but they can do very powerful things.

The following code restores the layout you had previously saved as <your layout name>MATLABLayout.xml.

(this goes in startup.m)

%% restore your custom layout
layoutName = '<your layout name>';
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;

Set layoutName to whatever you named your layout. Now to the restarting MATLAB part.

Remember MATLAB's preferences reset every time I reboot the computer at my workplace. The default MATLAB path (which can be found using the path command) only includes directories I do not have permissions to write to. This means I cannot put startup.m and finish.m on the MATLAB path, such that after a reboot they are still there. How do I solve this? I use neat MATLAB.exe command line trick to copy startup.m into a folder on the MATLAB path and then I run startup.m manually.

The shortcut to MATLAB in my Start Menu also resets after a reboot, so I use a .bat file on my desktop to run MATLAB as follows.

(this goes in MATLAB.bat, which can be placed anywhere you have easy access to, e.g. the Windows desktop or a personal folder)

@echo off
start path\to\matlab.exe -r "copyfile('path\to\startup.m', matlabroot), startup"

This will run MATLAB. Furthermore, the -r tells MATLAB to run whatever comes next between the "double apostrophes" after it has finished starting up (even after running startup.m, if it were existent, which is not the case in my situation). The part between the apostrophes is MATLAB code that tells MATLAB to copy the startup.m script we created to a place on the MATLAB path, in this case matlabroot. This can be any other directory on the default MATLAB path, or you could use addpath('path\to\add') to add any path to the MATLAB path. This could add the path to our startup.m. The last command between the apostrophes is simply a call of startup.m, so it runs.

Now, the script will set our desired path, settings and layout. One problem still persists: MATLAB needs a restart to actually change the layout. This is done using startup.m, finish.m and a second .bat file.

The second .bat file will kill MATLAB through the command line and start MATLAB again. startup.m must know it was first started and then run this .bat file. finish.m must clear this knowledge. Here's what I've done.

(this goes in restartup.bat)

@echo off
taskkill /f /pid %1
start path\to\matlab.exe -r "copyfile('path\to\startup.m', matlabroot)"

As you can see, taskkill will kill a process with a yet unknown (%1) process ID (/pid). This ID will be provided in the call of this .bat file by startup.m. However, this call must not happen if MATLAB was already restarted to restore the layout and preferences. So startup.m must create a memory of it being run already. I did this as follows.

(this goes in startup.m)

%% if startup hasn't run before, restart MATLAB
% sources:
% http://www.mathworks.com/matlabcentral/newsreader/view_thread/259557
% https://stackoverflow.com/a/92057

% this file is deleted by finish.m
name = 'hasStartedUp';
f = ['path\to\' name];
if ~exist(f,'file')
    fid = fopen(f,'wt'); % create an empty file
    pid = num2str(feature('getpid')); % get MATLAB's process ID
    system(['path\to\restartup.bat ' pid]); % run restartup.bat

The memory I gave startup.m is a file that, if existent, prevents the restarting of MATLAB. This file is called name. The current MATLAB's process ID is obtained with num2str(feature('getpid')). This is in turn passed to the call of restartup.bat, which in turn kills the MATLAB process. Remember this kill doesn't invoke finish.m, so we delete the memory file in that script, so if MATLAB is exited normally, the memory file will disappear and MATLAB knows to restart the next time it is run.

(this goes in finish.m)

%% delete memory file from startup.m
f = 'path\to\hasStartedUp';
if exist(f,'file')

Bonus for the long read: read here how I've edited startup.m to open default figures on the left half of the screen. Now, MATLAB's main window can sit nicely on the right half of the screen and figure windows don't overlap it (which they do, by default).

Now I've solved and therefore answered my two questions! I understand this is quite an elaborate answer and therefore a tough read, so feel free to ask any questions and post comments with suggestions with further improvement of my methods.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.