MATLAB 中有没有办法获取启动会话的用户的用户名?
我对Windows、Linux和Mac OSX的解决方案感兴趣。我想如果解决方案是特定于平台的,那么两种解决方案都可以按如下方式集成:
if ispc
user_name = % method 1
elseif isunix
user_name = % method 2
elseif ismac
user_name = % method 3
end
MATLAB 中有没有办法获取启动会话的用户的用户名?
我对Windows、Linux和Mac OSX的解决方案感兴趣。我想如果解决方案是特定于平台的,那么两种解决方案都可以按如下方式集成:
if ispc
user_name = % method 1
elseif isunix
user_name = % method 2
elseif ismac
user_name = % method 3
end
使用 Java 怎么样(适用于 MATLAB 支持的所有平台):
user_name = java.lang.System.getProperty('user.name')
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。
在 Windows 上获取它:
getenv('USERNAME')