gethostname.m

来自「This contribution provides functions for」· M 代码 · 共 44 行

M
44
字号
function hostName = gethostname
%GETHOSTNAME  Get host name.
%		HOSTNAME = GETHOSTNAME returns the name of the computer that MATLAB 
%		is running on. Function should work for both Linux and Windows.
%
%		Markus Buehren
%		Last modified: 30.03.2009
%
%		See also GETUSERNAME.

persistent hostNamePersistent

if isempty(hostNamePersistent)
  if ispc
    hostName = getenv('COMPUTERNAME');
  else
    hostName = getenv('HOSTNAME');
  end
  
  if isempty(hostName)
    % the environment variable above was not existing
    if ispc
      systemCall = 'hostname';
    else
      systemCall = 'uname -n';
    end
    [status, hostName] = system(systemCall);
    if status ~= 0
      error('System call "%s" failed with return code %d.', systemCall, status);
    end
    hostName = hostName(1:end-1);
  end

  % environment variable and system call might result different, so only
  % allow upper case letters
  hostName = upper(hostName);
  
	% save string for next function call
	hostNamePersistent = hostName;
else
	% return string computed before
	hostName = hostNamePersistent;
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?