Quickly changing projects in MATLAB
When working on several different projects in MATLAB changing from one project folder to another can be a hassle. Some projects are in a shared dropbox folders, others are in my main MATLAB directory. Most powerful text editors offer a way of quickly changing project directories without having to navigate the folder structure by hand.
This is my solution for MATLAB.
%
% Hacky way of making projects in MATLAB
%
% One hassle of MATLAB is that moving from one project to another is a hassle.
% With this script you only have to type:
%
% open_project 'my project'
%
% to move from the current directory to the specified directory. Projects are
% simply folders in the one of the 'project_folders'.
%
% To list all the currently detected projects use:
%
% open_project --list
%
% To move to your MATLAB user directory (~/Documents/MATLAB on linux) use:
%
% one_project mlhome
%
function open_project(project_name)
project_folders = {
'/home/tibo/Documents/MATLAB/scripts'
'/home/tibo/Dropbox/PoliMi'
};
if strcmp(project_name, '--list')
list_projects(project_folders);
return
end
if strcmp(project_name, 'mlhome')
fprintf('Changing directory to %s\n', userpath);
% We need to strip the trailing ':' from the userpath
user_path = userpath();
cd(user_path(1:end-1));
return
end
for k = 1:length(project_folders)
path = fullfile(project_folders{k}, project_name);
if exist(path, 'dir')
fprintf('Found project. Changing directory to %s\n', path');
cd(path);
return;
end
end
fprintf('Unable to locate project %s\n', project_name);
end
function list_projects(project_folders)
for k = 1:length(project_folders)
fprintf('Projects in <%s>:\n', project_folders{k});
folders = dir(project_folders{k});
for j = 1:length(folders)
if folders(j).isdir && ~strcmp(folders(j).name, '.') && ~strcmp(folders(j).name, '..')
fprintf(' %s\n', folders(j).name);
end
end
end
end
After populating the project_folders variables with the directories you use to store your projects you can use open_project project_name to move from on project folder to another. To see the list of directories detected by the tool run open_project --list.
This will only change the current directory to the project directory. It will not close existing editor tabs or clear the workspace.