How to find out %COMPUTERNAME% and %USERNAME% ??
24 次查看(过去 30 天)
显示 更早的评论
What the title says.
How do I find out on which machine (computer) MATLAB is running? (Windows CMD variable %COMPUTERNAME%)
How do I find out the name of the user who is logged in? (Windows CMD variable %USERNAME%)
The Matlab help is of no use since none of the search terms "user name", "machine name" and "hostname" lead me to anything useful...
many thanks in advance!
0 个评论
回答(5 个)
Daniel Shub
2011-9-1
With java you can do this in an OS independent manner:
char(java.lang.System.getProperty('user.name'));
char(java.net.InetAddress.getLocalHost.getHostName);
1 个评论
Ken Atwell
2011-9-1
GETENV is a somewhat more "graceful" way to do this:
getenv('COMPUTERNAME')
0 个评论
Fangjun Jiang
2011-9-1
or
getenv('username')
getenv('computername')
2 个评论
Walter Roberson
2017-1-7
This works fine on osx: it gives back the empty string in both cases, which is exactly what is appropriate when asked to provide system specific information. The question was not about how to fetch the username and computer name on Windows, it was about how to access those particular variables.
The osx equivalent variables are USER and HOSTNAME . USER should be available on on shells, but HOSTNAME might not be available if you have reconfigured the default system shell to csh or you dug out an old sh source somewhere and compiled that and made it your default system shell. Posix does not mandate that the environment variable exists... but the default shell for osx does define it.
I am not sure what the default shell is for the various Linux systems. I am sure it is not csh, but I cannot promise it is ksh or bash instead of sh.... Posix does mandate some shell features that were in ksh but not in sh, but that doesn't matter to Linux systems because Linux is not Posix.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!