Setting working directory to Documents folder in user profile on Windows 7

I would like to set the working directory to the Documents folder of the currently logged-on user on Windows 7 machines. I would like to do this in startup.m
I imagine something like:
username = getusername;
cd C:\Users\username\Documents
What would be the correct syntax or code in startup.m for this?
Thanks.

Answers (1)

I saw this function on the File Exchange, http://www.mathworks.com/matlabcentral/fileexchange/32523-getusername/content/getUserName.m, which pretty much does what you want in terms of getting the user name. With that, you can build a command string to change directories, and then evaluate that command
% get the user name
userName = getUserName();
% build the command string
command = ['cd C:\Users\' userName '\Documents'];
% evaluate the command
eval(command);
Try the above and see what happens!

4 Comments

When I used this code I got the following message in the command window in Matlab:
Warning: Executing startup failed in matlabrc. MATLAB:UndefinedFunction Undefined function or variable 'getUserName'.
Stephen - you need to get the code from the link and paste it in your startup.m file (or store the file elsewhere).
Or since you are using Windows, just try
userName = getenv('username');
No need for eval!
cd(fullfile('c:',username,'documents'))

Sign in to comment.

Categories

Products

Asked:

on 20 Jun 2014

Commented:

on 20 Jun 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!