Get user name from the command line on different platforms
-
12-12-2019 - |
Question
Is there a way in MATLAB to get the user name of the user that started the session?
I am interested in solutions for Windows, Linux and Mac OSX. I imagine that if the solutions are platform-specific, both solutions can be integrated as follows:
if ispc
user_name = % method 1
elseif isunix
user_name = % method 2
elseif ismac
user_name = % method 3
end
Solution
if isunix
[~, user_name] = system('whoami') % exists on every unix that I know of
% on my mac, isunix == 1
elseif ispc
[~, user_name] = system('echo %USERDOMAIN%\%USERNAME%') % Not as familiar with windows,
% found it on the net elsewhere, you might want to verify
end
Hope that helps! You might want to also put in a else I'm confused
clause just in case you do find that system that is not unix nor pc.
OTHER TIPS
How about using Java (works on all platforms supported by MATLAB):
user_name = java.lang.System.getProperty('user.name')
To get it on Windows:
getenv('USERNAME')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow