View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Get computer name/hostname

3.3 | 11 ratings Rate this file 17 Downloads (last 30 days) File Size: 1.26 KB File ID: #16450 Version: 1.1

Get computer name/hostname


Manuel Marin (view profile)


19 Sep 2007 (Updated )

It provides the computer name where running. Useful for customizing scripts depending on the machine

| Watch this File

File Information

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


> mycomputer = getComputerName();


This file inspired Get User Name.

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (19)
08 Feb 2017 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.

26 Sep 2016 K E

K E (view profile)

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

Comment only
22 May 2012 Casper

Casper (view profile)

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

This is not worth a function.

Comment only
16 Feb 2012 Abhishek

13 Aug 2011 Felipe G. Nievinski

30 Jul 2011 MathMoose

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

Comment only
26 May 2011 Noah

Noah (view profile)

22 Apr 2011 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.

Comment only
13 Apr 2011 Matthew Crema


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).

11 Mar 2011 Ken

Ken (view profile)

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

Comment only
10 Mar 2010 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);

Comment only
30 Jul 2009 Richard Crozier

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!

28 Jul 2009 Cesare

Cesare (view profile)

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

01 Sep 2008 Alex Leung

Absolutely no problem at all!



20 Sep 2007 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.

20 Sep 2007 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.

19 Sep 2007 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.

Comment only
19 Sep 2007 Marcelo Perlin

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

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

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

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

My suggestion is to delete it...

Comment only
21 Sep 2007

Help improved

12 Dec 2013 1.1

As suggested by some users, insignificant whitespaces are removed.

Contact us