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:
Let's reproduce that.
- Simply drag the main MATLAB window to where you want it, e.g. maximise it. Solved!
- Click the arrow icon right beneath the red exit × in the top right corner of the MATLAB window to collapse the ribbon. Solved!
- 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.
- I put the Workspace next to the Current Folder tab. Solved!
- Simply click on the × to the right in the yellow tip's bar.
- 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.
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
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:
- Set the Current Folder to whatever directory we want after startup.
- Overwrite the default MATLAB preferences with our own, as saved in Part 1 of this answer.
- 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
%% 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
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
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
%% 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
%% 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
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
%% restore your custom layout
layoutName = '<your layout name>';
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
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
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
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)
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
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
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
%% if startup hasn't run before, restart MATLAB
% this file is deleted by finish.m
name = 'hasStartedUp';
f = ['path\to\' name];
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
%% delete memory file from startup.m
f = 'path\to\hasStartedUp';
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.