function write_dem(filename, DEM, xllcorner, yllcorner, cellsize, nodata) % read_dem(filename, dlm) outputs a DEM and a DEM with NaN values from an % ASCII file exported from ArcGIS % filename = 'filename.txt' % dem = matrix to be made into ascii file % xllcorner = lower left corner in x (I use UTM, but I think % longitude would work) % yllcorner = lower left corner in y (I use UTM, but I think % latitude would work, too) % cellsize = distance between grid/matrix points (m) % nodata = the integer that is represents no data (uses -9999 if % NaN is entered) % Format of text files % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % ncols m % nrows n % xllcorner 502035 % yllcorner 6622425 % cellsize 30 % NODATA_value -9999 % row 1 col1 [space] col2 [space]... colm [space-return] % row 2 data [space-return] % ... % ... % ... % row n data [space-return] % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % Does the matrix have nan values? if isnan(nodata) == 1 nodata = -9999; DEM(find(isnan(DEM)==1)) = -9999; end % Define the different values to be used [num(2), num(1)] = size(DEM); num(3) = xllcorner; num(4) = yllcorner; num(5) = cellsize; num(6) = nodata; % define strings for headings str{1} = ['ncols']; str{2} = ['nrows']; str{3} = ['xllcorner']; str{4} = ['yllcorner']; str{5} = ['cellsize']; str{6} = ['NODATA_value']; % make a file with the proper text values and such fid = fopen(filename,'wt'); for j = 1:1:6 if j == 1 || j == 2 || j == 6 fprintf(fid, '%s %d\n', str{j}, num(j)); % put in as integers else fprintf(fid, '%s %f\n', str{j}, num(j)); end end fclose(fid); % Write DEM into the proper format (flipud first) DEMud = flipud(DEM); dlmwrite(filename, DEMud, '-append','delimiter', ' ', 'precision', '%.4f', 'newline', 'pc') % this uses 4 decimal places