matlabplatform-independent

Get user name from the command line on different platforms


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.