How to get the account username using MATLAB?

I am talking about the username, not the host or computer name. For instance, if you are working on Google Drive, your Drive folder will be under C://users/USERNAME. I am talking about that USERNAME part. :)

1 Comment

If u run command prompt (windows button + R ------> type 'cmd' and press 'Enter'), the default command line shows the username (image below)

Sign in to comment.

 Accepted Answer

Chuck
Chuck on 21 Feb 2016
Edited: Chuck on 21 Feb 2016
Found it! :) It's extremely straight-forward.
Let me post it here for those who are struggling to find:
getenv('username')

4 Comments

On OS-X and Linux it would be
getenv('USER')
If you are looking for the home folder then on OS-X and Linux,
getenv('HOME')
and on MS Windows,
[getenv('HOMEDRIVE') getenv('HOMEPATH')]
is there a way we can retrive the user's Full Name?
Which operating system, Ram? The solution for Windows is likely to be quite different than Linux or Mac.
Note that the solutions might be different for network login situations.
For Mac it looks like you could use
[status, fullname] = system('id -F')
but watch out as it will include line terminator

Sign in to comment.

More Answers (2)

To get the folder, you can use
userProfile = getenv('USERPROFILE');

2 Comments

I guess this is for Windows .. Do you by chance know how to get the userprofile on linux and mac ?
Mac and Linux do not have user profile. See https://www.mathworks.com/matlabcentral/answers/269214-how-to-get-the-account-username-using-matlab#comment_344298

Sign in to comment.

userName = char(java.lang.System.getProperty('user.name'))
userName = 'mluser'

4 Comments

getenv and char(java.lang.System.getProperty('user.name')) does not work for matlabwebapps, what should we use in that case?
Web apps run on the server. The "user" is the name of the account that is acting for the server process. web apps only display on the user end, not run there. The individual user is not logged in, so asking about their username is not useful.
@Francisco Sacchetti What are you hoping or planning to do with that information in a web app?
I found a path:
  • Enable SSL on the server. For more information, see Enable SSL.
  • Enable authentication on the server. For more information, see Authentication.
And then you can use compiler.UserInfo() to get the infromation of the user.
I want to first to track who is using the app, also it will be nice to adapt the app depending on the users.

Sign in to comment.

Products

Asked:

on 21 Feb 2016

Edited:

on 12 Oct 2022

Community Treasure Hunt

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

Start Hunting!