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!

回答(5 个)

Daniel Shub
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);

Ken Atwell
Ken Atwell 2011-9-1
GETENV is a somewhat more "graceful" way to do this:
getenv('COMPUTERNAME')

Fangjun Jiang
Fangjun Jiang 2011-9-1
or
getenv('username')
getenv('computername')
  2 个评论
Walter Roberson
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.

请先登录,再进行评论。


David
David 2011-9-1
Ooooh never mind I found the answer...
>> [s,computername] = dos('ECHO %COMPUTERNAME%')
s =
0
computername =
VARESE
>> [s,username] = dos('ECHO %USERNAME%')
s =
0
username =
myusername
:)

David
David 2011-9-1
Thanks for the tips! getenv seems like just the thing I was looking for. (All comps are Windows, software won't be shipped out any time soon...)

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by