File Exchange

image thumbnail

Get computer name/hostname

version 1.1.0.0 (1.26 KB) by Manuel Marin
It provides the computer name where running. Useful for customizing scripts depending on the machine

7 Downloads

Updated 12 Dec 2013

View License

GETCOMPUTERNAME is a very simple but useful function to get your computer name, i.e. the hostname.

Example:

> mycomputer = getComputerName();

Cite As

Manuel Marin (2019). Get computer name/hostname (https://www.mathworks.com/matlabcentral/fileexchange/16450-get-computer-name-hostname), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (21)

Jonathan

Computer name and hostname are different on OSX (see scutils), and on certain Linux distros (don't know about Windows).
This function literally just calls "hostname" for you in the background, and uses COMPUTERNAME/HOSTNAME env-vars in case of failure. If you know your platform, that's a one-liner; and your code would probably be clearer if you did that yourself.

Rob Campbell

Fails on Mac and Linux despite ispc check.

Patrick

I understand that for most people this function may be "trivial" but for someone who is new to Matlab this provided me a solution to a question I had. Manuel thank you for posting this. Also it's not like matlab is gonna run out of space.

K E

Casper's suggestion should read,
[~, name] = system('hostname');
It works fine by the way.

Casper

Just this works fine.
[~, name] = system(hostname');

This is not worth a function.

Abhishek

MathMoose

@Ken Why is strtrim better? Are you having problems with whitespace at the beginning of your hostname string?

Noah

Peter Raeth

Many thanks to Manuel Marin for posting this information.

As an aside, things like this are only "trivial" to those who already know the answer.They are critical to those who are searching for answers.

A spark should never be put out. Rather, it should be fanned. Posters should be encouraged in their attempts to serve the community.

Helpful.

Maybe useful to put this at the end
hostname = hostname(hostname ~= 10);

to remove the newline character at the end of the string (on the one computer I've tested it on).

Ken

For future reference (with regards to MathMoose's suggestion), consider using strtrim where applicable instead of deblank.

MathMoose

Thanks. I had to strip a newline character from the end of the name string returned (on Windows 7), but this function was useful.

I added to the end of the file:

% remove trailing newline or null characters
name = deblank(name);

Marcelo and Jos, I for one find this a really useful function which saved my looking up the obscure (to me) functions used within it. This has saved me probably at least an hour of searching through help. So basically you can get lost.

Manuel do not delete the file, it's helped me, thank you!

Cesare

I think it was usefull. I didn't know how to do it, now I do.

Alex Leung

Absolutely no problem at all!

AHMET ANIL D?NDAR

THANKS FOR THE CODE

m m

If ret ~= 0, it means that command 'hostname' has not been found in the system. So, an alternative way, based on enviromental variables, is tried. On the other hand, 'name' always is set to a value, either by system() or by getenv.

Jos x@y.z

This (indeed trivial) function will produce a misleading error ('Undefined function or variable "name".') if "ret", for whatever reason, equals zero. If ret is always non-zero, why check? And why convert it to lower case?

The help could be improved, by adding a H1, SEE ALSO lines.

Manuel Marin

Thanks for your comment but, from my point of view, if I have to rewrite 10 lines of code hundreds of times, independently of the difficulty, it worths to pack them into a function.

Marcelo Perlin

Manuel, you can't publish something as trivial as this:

[ret, name] = system('hostname');

if ret ~= 0,
if ispc
name = getenv('COMPUTERNAME');
else
name = getenv('HOSTNAME');
end
end
name = lower(name);

I don't see how you can improve this because it is very trivial.

My suggestion is to delete it...

Updates

1.1.0.0

As suggested by some users, insignificant whitespaces are removed.

1.0.0.0

Summary updated

Help improved

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: getUserName