6

MATLAB 中有没有办法获取启动会话的用户的用户名?

我对WindowsLinuxMac OSX的解决方案感兴趣。我想如果解决方案是特定于平台的,那么两种解决方案都可以按如下方式集成:

if ispc
    user_name = % method 1
elseif isunix
    user_name = % method 2
elseif ismac
    user_name = % method 3
end
4

3 回答 3

13

使用 Java 怎么样(适用于 MATLAB 支持的所有平台):

user_name = java.lang.System.getProperty('user.name')
于 2012-07-26T16:31:26.367 回答
4
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

希望有帮助!您可能还想加入一个else I'm confused子句,以防万一您发现该系统既不是 unix 也不是 pc。

于 2012-07-26T16:07:39.813 回答
1

在 Windows 上获取它:

getenv('USERNAME')

于 2014-07-23T20:30:56.880 回答