File Exchange

image thumbnail

READHGT: Import/download NASA SRTM data files (.HGT)

version (64.7 KB) by François Beauducel
Imports or download SRTM worldwide digital elevation model data files.


Updated 28 May 2020

View License

This function imports .HGT "height" binary data files from NASA SRTM global digital elevation model of Earth land, corresponding to 1x1 degree tiles of 3-arc seconds resolution (SRTM3, around 90 m) and 1-arc second (SRTM1, around 30 m), and returns coordinates vectors latitude and longitude, and a matrix of elevation values.
The function includes also an automatic download of data from the USGS SRTM webserver, so indicating latitude and longitude is sufficient to get the data and instant map plot anywhere in the World.
Few examples:
plots a map of Seattle and surrounding volcanoes,
X = readhgt(48,2,'plot');
plots a map of Paris (France) and returns downloaded SRTM data in structure X.
Type "doc readhgt" for syntax, options, other examples and use.

Cite As

François Beauducel (2020). READHGT: Import/download NASA SRTM data files (.HGT) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (81)

The files work splendidly for me when I use MATLAB scripts. However, I'm trying to obtain height data by feeding longitude and latitude through a MATLAB function block in Simulink. This introduces multiple errors, like non-constant expressions/empty matrices, expected scalars, functions which are not supported, or undefined functions/variables. Some of these can be solved by adding additional functions, like all( ), but I suppose there's a bigger issue with conversion to Simulink compatible code or directory problems behind it. Do you by any chance have any experience to share regarding the use of the code in Simulink?

Ashley How

Ashley How

Thank you Francois for the response! I will give it a go! :)

Dear Ashley,

Thanks for warning us about the neoknet website. Actually the only way to get SRTM1 tiles is to freely register to NASA/EarthDATA center ( and get the tiles from USGS. When it's done, you have 2 solutions :
- get the data manually and use readhgt.m with 'outdir' option pointing to the right directory;
- use the new 'login' option I introduced in version to specify your user and password on the command line.

I made a try with a part of Guatemala accessible here :

Ashley How

The website no longer appears to be accessible, so higher quality resolution data isn't available. When running the script from tiles downloaded from the site, there are discrepancies in the data (blobs that show up on the maps, etc.).

I am trying to get a 30m resolution SRTM map for Guatemala - does anyone know of an alternative website which might provide this, considering the USGS website only provides SRTM1 data for the US. I am using Matlab 2020a.

Eric Hayoz


NOTE: the NASA data used here has not been post processed to fill in missing SRTM data, which there is lots of.
So expect to see lots of artefacts...!
This tool has its own interpolation but it will not compare to a properly filled data source, such as this one:
There are probably even better SRTM hole filled data sets, particularly for smaller regions like Australia, but i have not yet found one with a nice data format that is as easy to import as the one above.
This should be a disclaimer in the description...

Dear Zaidan,
I just published a new version that should work with your 2017a.

hi every one,
i have an issue aboust using function readhgt.m , when i want to use it like the example X = readhgt(48,2,'plot') , i got an error " tile not found , Considering offshore ".
after i read many response before, i have tried to download wget , than i change unzip to unzip1 like Mr.Tim paseman said.

PS :
- sorry if my english is bad
- i use matlab 2017a and windows 10

Dear Tolga,
Well this label issue comes from your "locale" language installed on your computer and it happens for recent release that force UTF8 character encoding. You might install also ISO character set to solve the problem. If you want a quick turn around, you can also edit dem.m and edit the degree character at line 963 into the sprintf() function,
To plot the fault, the simplest would be to use the patch() command. Take a look to an other function I wrote : .

Hi there everyone,
I just came accross to this usefull thing, yet encountered a little problem. As I try your code on 2018b platform, the latitude and logitude labels are not like they are
supposed to be. My area of intrest was lat: 38.0 : 42.0, lon: 31.0 : 34.0 (Central Anatolia). And the labels are something like 32AE, 33AE,...
Has anyone encountered such a problem? Is this something particular with 2018b? How about an old Matlab version like 7.0.1 (back to the baby-Matlab days).
What will be next for me is to plot some fault segment locations on the DEM figure I create. A few .kml files involving the latitutes and the longitutes of the fault locations are input via 'read_kml.m (' function and a hold on command preceeding a
'plot(lonfault,latfault)' looks like adequate. Any other suggestions are wellcomed.

Best regards.

Calvin Hill

Thanks for the update François

Dear Calvin,
I just published a new release with active decimation for output grid. Thanks for the suggestion.

Calvin Hill

Why does the 'decim' input for readhgt not change the size of my X structure?,X.lon and X.z always end up as 1201x1/ 1201x1201 regardless of what N is for input 'decim',N

Brian Dolan

Great code! I am wondering, however, why it seems not to work with negative longitudes:

minlong = -123;
maxlong = -114;
minlat = 31;
maxlat = 38;

readhgt([minlat, maxlat, minlong, maxlong])

Error message

Subscripted assignment dimension mismatch.
Error in readhgt (line 461)
Y.z((sz(1)-1)*(X(n).lat(1) + (1:sz(1)),(sz(2)-1)*(X(n).lon(1)-Y.lon(1)) + (1:sz(2))) =

paul davis

The problem of "tile" not found is due to Matlab unzip function. The solution is to use websave function to download file, then use unzip to downloaded file. Creat unzip1 function as below and replace all unzip function.

function y = unzip1(url,out)
[pathstr,name,ext] = fileparts(url) ;
outfilename = [out '/' name];
outfilename1 = websave([outfilename ext],url);


OK, I see that it's because WGET needs to be installed - as the author describes in the script notes.


this file used to work brilliantly for me. I used it a lot about 18 months ago.
Nowadays if I insert, for example, X = readhgt(48,2,'plot'); it returns a message "Download ... ** tile not found. Considering offshore." and gives a blank/blue plot.
I can get the requested file manually by going to the URL.
Using a Mac OSX 10.11 and Matlab ver 2017a if that is relevant.

Additionally you have to delete the logic for handling srmt1 and srmt3 files to just use the srmt3 files. You can find that @ line 350

Alright alright for everyone who was Problems with the download not working. It CAN be an issue with the https-stuff. If you use Windows you can go to line ~180 (+ -) and Change the Code block to the following:

srtm1 = any(strcmpi(varargin,'srtm1'));
if srtm1
% EXPERIMENTAL: SRTM1 full resolution tiles available here (2016):
%url = '';
url = '';
% official USGS SRTM3 tiles (and SRTM1 for USA):
%url = '';
%using different url:
url = '';

neoknet-URLs work for me... might be an idea for others.

du yanshen

Is it posible to plot maps in 3D?

Toni Wöhrl

I can only get readhgt to import directly from the internet if I use the 'url' option with the full path name.

Ok. I figured it out. It helps to specify West longitudes as negative inputs. However, I still had to download the tiles to a local folder in the matlab path and use the 'outdir' option to make it work.

Excellent tool François!
I am having difficulty retrieving srtm1 data. I get the following error using the example provided or any other request for srtm1:
>> readhgt([-16.4,-16.2,-71.5,-71.3],'srtm1','interp')
Download ... ** tile not found. Considering offshore.
Is there a workaround for this?
Thanks again,

Hello - I am working on a project that would benefit from global elevation data. I apologize in advance if this question is redundant.I gave this a try, but when I run your example, "X=readhgt(46:47,-123:-122);" The data is sourced and a message at the end of the printed sourcing lines appears: "READHGT: In the figure data has been decimated by a factor of 2..."

The plot appears and is all light blue - every elevation data point is 0 m. I display X.z and it all comes back as 0....

Please advise? What have I done incorrectly?

Jim Pincini

This appears to be amazing. Thank-you!!

for colormap and other plot options, please refer to the dem function and post comment on the appropriate page, not readhgt.m

Im almost there, just need to figure out how to make the colormap gray, but it will have to wait til tomorrow. Thanks again for help.

Hmmm, actually that resulted in a very low resolution image file. I dont own a Mac and my linux box is my kids comp, but I opened it in photoshop and yea its small.

I dont use your dem color mapping, just a grayscale colopmap to create a height map. Print only ever seems to print the current figure display resolution, not the full depth of the data when zoomed in. Even when I force the resolution, no effect.

Best way to export the whole figure is Encapsulated PostScript format:

print -depsc -painters -loose figure.eps

The output can be natively read with Linux and MacOS, and converted to image using ImageMagick convert command, e.g.:

convert export.eps export.jpg

or try .pdf to keep the vectorial format.

If you just want the shaded relief image itself:


Thanks for the answer, the image came in beautifully.

I am having issue exporting the surface data to an image at native resolution. I have tried the print function and export_fig but the resolution always gets reduced on output. I tried imwrite() as well, but the data is a structure type, not an integer. Any insight again would be greatly appreciated.


Dear André

Thanks for your feedback and sorry for my late reply: Mathworks seems not sending "watching e-mails" anymore...

The 'nodecim' option is only valid for the dem.m plot function. With readhgt.m, you must use readhgt(...,'decim',1) to force the highest resolution.

Regarding your second question, SRTM data are lat/lon grid so each pixel has different size in km. readhgt.m uses my dem.m function to plot a relatively realistic X/Y ratio to respect distances, i.e., it shrinks longitudes while absolute latitudes are increasing. If you want to overwrite that, you may do:


See my dem.m function documentation for further options.


Great tool! I am very new to matlab and I am managing my way though great because of this tool.

I have a few question if I may. I want to import an area that encompasses a range of a few degrees of lat and longitude using the STRM3 dataset but when I use the 'nodecim', it still decimates the data by a factor of 8. Could you provide an example of the correct syntax?

Also when I import an individual tile I was under the impression that the file would be 1201x1201 pixels yet when the figure comes in its rectangular.

I am very new to matlab so please excuse my ignorance.



Dear François,

Using SRTM3 tiles instead of SRTM1 worked perfectly.

Indeed, the scarce memory resources of my computer were the cause of the problem.

Many thanks once again and congratulations for developing such a powerful tool.


Dear Cebri,

This is not really a Matlab problem but a computer memory problem.

The area you want to download is too huge and the matrix itself (32401 x 79201 !) exceeds your RAM computer capacity.

I would recommend then:

a. to not use the 'srtm1' option: default SRTM3 will make a beautiful 10801 x 26401 image which might be sufficient for your needs;

b. to split the area in 4 tiles or even more so your computer can manage it one by one.


but anyway you may have problems to open the image at the end with your present computer.


Dear François,

Thanks a lot for updating the url protocols.

I am having problems with computer memory when I attempt to plot maps that cover large extensions of land. The function is always interrupted because MATLAB reaches the maximum array size limit.

I need to plot a map that extends from Latitude 32ºN to 40ºN and from Longitude 68ºE to 89ºE.

I am calling READHGT like this:
readhgt([32, 40, 68, 89], 'interp', 'srtm1', 'plot');

But after a few minutes of processing, MATLAB throws the following error message:

"Requested 32401x79201 (4.8GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive."

I would be grateful if you could provide any piece of advice in order to overcome this memory problems so that I can get a plot of the map.

For reference, I am running a 64-bit Windows 8.1 with 4 GB RAM.

Kind regards.


Dear Cebri,
Thanks very much for the information. After a few investigations, it seems that USGS made a redirection from http:// to https:// URL, which is not handled by unzip Matlab's function (while it is using Octave!).
I will continue the checks and propose very quickly a new version.


Dear François

I am also experiencing the same behavior as Shin Gwangsoo:

I have tried different coordinate ranges but I always get "tile not found. Considering offshore". So I end up having all 0 values for z.

Then I have executed every example in the source file and the only one that is working is the Misti volcano, Peru. For all the other examples I get "tile not found. Considering offshore".

I realized that for downloading the data, the Misti volcano example uses the url: "" while all the other examples use the url: "".

I accessed the url: "" via web browser and I could navigate through the directories and files without any problem.

I don't know where the problem might come from.


- for non-USA SRTM1 I just updated the URL to avoid the message "tile not found"
- for default SRTM1/3 tiles, remember you need internet connection to download data AND a writable current directory.

Thank you:-)

I want to play this code.

but, eroor..

"tile not found. Considering offshore..."

Quite remarkable -- thank you very much. I look forward to learning how to contour the data.


A great resource. Thank you!!!

Dear Jeremiah,

Thanks for your feedback.

Actually the height values of SRTM data are elevation above the geoid and not ellipsoid. The geoid is equivalent to the long-term mean sea level.

Converts these data to ellipsoid reference like WGS84 is quite easy to do... good suggestion ! for a future release ?


Thank you for the awesome file.

I was hoping for clarification on what height is reported. In reading about SRTM it appears height is listed either as height above the WGS84 ellipsoid or the EGM96 ellipsoid. Your code says it returns height above sea level.

Could you confirm which height you are returning? Also, would it be possible to have an option to return height above WGS84 ellipsoid? The offset is given in the data.

E. Cheynet

Dear akfarell,
If you need the full indexing, you must use the meshgrid function:
then lon,lat and X.z have the same size and indexed the same way.


Dear Francois,
Just to clarify, how is the z matrix indexed with reference to the latitude and longitude values? Is it (lat(1:end), lon(1:end))? Thank you!



thank you

Dear Francois,
Thanks for the suggestion,

vinod pillai

Dear Vinod,
SRTM .hgt files are freely available at
Once downloaded, a single tile (1 by 1 degree) is coded as a simple matrix of 1201 by 1201 binary int16 values, big-endian. No header. Integer values represent elevations in meter. -32768 value means "no data".
For SRTM1 tiles (USA territories), tiles are 3601 by 3601.
That's all. See online documentation for further details but this is easily readable in any language.

Dear Francois,

I joined Matworks today only, while searching for some help on extracting height from SRTM file, and happen to catch your link. Though i have not worked on MATLAB, is there any other way ( say in VB.NET) to capture the height from SRTM .hgt.




Thanks, saved me some work :-)

Dear John,
Yes, you missed the required version of Matlab (2014a) which contains a new function "strjoin".
I just updated the script to improve compatibility with older versions of Matlab. Please let me know.


This seems to be a great tool, but I am having issues getting it to work properly. I've tried many of the examples from the help. All of them are returned with:

Undefined function 'strjoin' for input arguments of type 'cell'.

Error in dem>checkparam (line 976)
mes = sprintf('%s (%s)',mes,strjoin(val,' or '));

Error in dem (line 415)
[s,axeq] = checkparam(varargin,'axisequal',@ischar,{'auto','manual','off'});

Error in readhgt>fplot (line 395)

Error in readhgt (line 350)

Is there something I am missing?

thanks Daniel.


Thank you very much for this submission.
This is an awesome tool.



Great tool, definitely useful!

I discovered one issue when downloading hgt files of North America data at all latitudes above 54°N.

On the USGS website these files are not in the * format, and are instead * (no decimal point before hgt).

Therefore line 236 in 'readhgt.m' fails for these latitudes:
k = find(~cellfun('isempty',strfind(idx{1},ff)));

I changed this line to:
k = find(~cellfun('isempty',strfind(idx{1},ff(1:end-8))));

which works, but might not be the most elegant!


Peter Nave

Very useful tool, excellent documentation!


Regarding my previous comment, I just now noticed you recently added the 'crop' argument update which is exactly what I needed. You are way ahead of me... Amazing what a little bit of reading can do :) Thanks.

A user asked me how to extract an elevation profile from SRTM data... here is a little example (Mount Etna, Sicily):

X = readhgt(37,14:15,'merge','plot');
[x,y] = ginput(2)

then left-click on 2 different points: you will get 2 coordinates in x and y vectors. Then interpolate to create a profile vector:

xi = linspace(x(1),x(2));
yi = linspace(y(1),y(2));
zi = interp2(X.lon,,double(X.z),xi,yi);

note you need to use the DOUBLE command since SRTM elevation data are int16 class. Plot the profile on the DEM:

hold on,plot(xi,yi,'r'),hold off

Since x and y are longitudes and latitudes, the linear profile is an equirectangular approximation (rhumb line or loxodrome) of the great circle... To compute distance along this path:

di = sqrt(((xi-x(1)).*cosd(yi)).^2 + (yi-y(1)).^2)*6370*pi/180;

where cosd(yi) reduces longitude distance versus latitude, and last factor is latitude degree length in km. Then plot the profile:


that's all. If you know the profile coordinates, you don't need to use GINPUT and you directly define xi and yi with LINSPACE.

Thanks Warwick for your kind feedback. I'll add a 2x2 tile option, in few days.


thank you. this is terrific, and surprisingly fast. A suggestion for a small increase in quick and easy usability would be an example of a 2by2 tile.


new option 'login' to specify user and password at NASA/EarthDATA center (free registration needed) in order to download SRTM1 tiles (in addition to 'srtm1' option).

- replaces unzip with webread function, a workaround to automatic download failure

- 'decim' option applies also to output grid(s).

adds new option 'wget' to workaround an issue with older releases (< 2014b), when downloading tiles automatically. This option works only for Linux and MacOSX. See documentation for further explanation.

MAJOR UPDATE: changed URL protocol to access USGS SRTM3 files. This is to avoid "offshore tiles" message when downloading new files. An alternative is to always use the URL option:

updates URL for world-wide SRTM1 tiles. Still unofficial...

- fix issue when merging SRTM1 tiles

- fix a problem when SRTM1 tiles are not found.

- fix in AREA syntax

- new options 'outdir' and 'url' (replacing former syntax)
- improves documentation

- new syntax readhgt(AREA)
- now can download SRTM1 30-m resolution tiles worldwide with option 'srtm1'

- 'merge' option is now default. Use new 'tiles' option to plot/download individual tiles when lat/lon are vectors
- LON argument is now automatically normalized to -180/+179°
- dem.m companion function update

improve backward compatibility (strjoin)

corrects an issue with tiles above latitude 54N (thanks to Greg).

- bug correction in case of READHGT(FILENAME)
- rewriting of help
- minor corrections

- new 'interp' option that fills the novalue gaps
- new 'crop' option to select rectangle part of tile(s)
- updated DEM companion function

minor update

- improves coding
- upgrades companion dem.m function

Bug correction when "dem.m" function is not detected.

- minor modifications
- now uses DEM function (included in ZIP file) for basemap axis and legends

- adds possibility to download multiple tiles with merge option
- improved graphic output

- adjust aspect ratio with latitude
- add minimal basemap axis ticks

- now reads also SRTM1 USA territory (1 arc-second resolution) data files.
- minor bug corrections.

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