3.66667

3.7 | 6 ratings Rate this file 52 downloads (last 30 days) File Size: 490 Bytes File ID: #16450

Get computer name/hostname

by Manuel Marin

 

19 Sep 2007 (Updated 21 Sep 2007)

No BSD License  

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

Download Now | Watch this File

File Information
Description

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

Example:

> mycomputer = getComputerName();

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
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');
   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...

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.

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.

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.

19 Jul 2008 AHMET ANIL D?NDAR

THANKS FOR THE CODE

01 Sep 2008 Alex Leung

Absolutely no problem at all!

28 Jul 2009 Cesare

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

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!

Please login to add a comment or rating.
Updates
21 Sep 2007

Help improved

21 Sep 2007

Summary updated

Tag Activity for this File
Tag Applied By Date/Time
hostname Manuel Marin 22 Oct 2008 09:27:37
operating system Manuel Marin 22 Oct 2008 09:27:37
computer name Manuel Marin 22 Oct 2008 09:27:37
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com