Code covered by the BSD License  

Highlights from
ARCGRIDWRITE

4.4

4.4 | 5 ratings Rate this file 35 Downloads (last 30 days) File Size: 2.68 KB File ID: #16176

ARCGRIDWRITE

by

 

30 Aug 2007 (Updated )

Write gridded data set in Arc ASCII Grid Format

| Watch this File

File Information
Description

ARCGRIDWRITE converts data in a matlab grid (as produced by eg. meshgrid and griddata) into a text file in Arc ASCII Grid Format. The file can also be automatically converted to raster on PC's with ARCINFO installed.
 
EXAMPLE - create a raster grid of the peaks function
[X,Y,Z]=peaks(100);
arcgridwrite('peaksArc.asc',X,Y,Z,'precision',3, 'convert')
 
NOTES
Because the Arc ASCII format has only one parameter for cell size, both X and Y must have the same, non-varying grid spacing.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
23 Nov 2013 Yugarshi

in Marc's code:

replace:

for i=1:mz

with

for i=mz:-1:1

this fixes the flipud problem

02 Jul 2013 Marc

Thanks for this useful submission. However the performance of your code is bad (at least on my (standard) pc). Writing of a 3500 by 6000 matrix would take days on my desktop machine. I looked into your code and replaced your nested for loop (which is a real bottleneck!) containing the fprintf statement by a single one, taking advantage of the list behaviour of the format specifier the fprintf command can take.
specifically i replaced:

%%%START OF YOUR ORIGINAL CODE%%%
%write data
for i=1:mz
for j=1:nz

if j==nz
fprintf(fid,[dc,'\n'],Z(i,j));
else
fprintf(fid,[dc,'\t'],Z(i,j));
end
end
%update waitbar
waitbar(i/mz,h,['Writing file: ',[fname,ext],...
sprintf(' %d%% complete...',round(i/mz*100))])
end

%%%END OF YOUR ORIGINAL CODE%%%

with:

%%%START OF MY MODIFIED VERSION%%%%
test=repmat([dc,'\t'],1,nz);
test(end-1:end)='\n';
%write data
for i=1:mz
fprintf(fid,test,Z(i,:));
%update waitbar
waitbar(i/mz,h,['Writing file: ',[fname,ext],...
sprintf(' %d%% complete...',round(i/mz*100))])
end
%%%END OF MY MODIFIED VERSION%%%

My modified Version takes approx. 10 minutes to write my large array. Maybe you can integrate this in your submission... By the way in ESRI arcmap10 i still have to flipud the imported asc file to display correctly...
Best regards
Marc

22 Jan 2013 Juernjakob Dugge  
02 Jun 2012 Pasha

Like Edward, I need to flipud my Z, but otherwise works perfectly!

26 Apr 2012 Dan

I really like this file and it works great on my Matlab desktop, but I am now trying to run it over a UNIX platform, which is a -nodisplay platform. I get an error saying that this functionality is no longer supported under -nodisplay.

Do you know if there is a solution where I could use arcgridwrite with -nodisplay?

Thanks,
Dan

26 Jan 2012 A Westergaard

The above mentioned matrix has 350 rows and 300 columns. Values are double.

25 Jan 2012 A Westergaard

Andrew,
Thank you for sharing your work. As I am new to MatLab, I hope you or others have time for some guidance: In Matlab I have a matrix with values [-1:1]. I want to export this matrix to ArcMap. What would the syntax be for this? Thank you in advance.

31 Mar 2011 Andrew Stevens

Edgar,

The default precision for the function is %0.3f, so you should get 0.075 as the cell spacing. I tried the following code to verify:

%generate synthetic data with desired %cell spacing
xi=0:0.075:1;
yi=0:0.075:1;
[xm,ym]=meshgrid(xi,yi);
zm=peaks(numel(xi));

%call arcgridwrite
arcgridwrite('test.asc',xm,ym,zm);

%look at output
type('test.asc')

The results look correct to me. Let me know (by email) if you have further questions.

31 Mar 2011 Edgar

Andrew,

Thanks for the script. However, I found the script is not able to write high resolution cellsizes. My grid has a resolution of 0.075. However, the scripts writes: "0.000". How can I solve this?? Thanks.

EM

08 Sep 2010 Edward Bair

Thanks for the util. It may be the way I'm building the grid, but ARCGRIDWRITE creates rasters that are flipped vertically for me. If Z is my raster, I need to do the following to have it written correctly:

[iv,jv]=ndgrid(1:size(Z,1),1:size(Z,2));
arcgridwrite('VegGrid_small.asc',jv,iv,flipud(Z));

09 Sep 2009 Daniya  
31 Mar 2009 Andrew Stevens

Colin,

Thanks for your comments. I found after playing with a couple examples, I determined that if I simply change the output filename from .txt to .asc that the file can be read directly into arc without doing the raster conversion.

I would also be careful with your modification of the way the header is written in the output file. Your modification forces the cell-size to be an integer which would produce a large error in many of my datasets.

Andrew

13 Mar 2009 Colin Lazauski

This is a nice file and works well. Modification of the header section write format will allow the file to be read directly by ESRI Arc-GIS. My modification is:

%write header
fprintf(fid,'%s','ncols ');
fprintf(fid,' %d\n',nz);
fprintf(fid,'%s','nrows ');
fprintf(fid,' %d\n',mz);
fprintf(fid,'%s','xllcorner ');
fprintf(fid,[dc,'\n'],minX);
fprintf(fid,'%s','yllcorner ');
fprintf(fid,[dc,'\n'],minY);
fprintf(fid,'%s','cellsize ');
fprintf(fid,'%-6.0d\n',dx);
fprintf(fid,'%s','NODATA_value ');
fprintf(fid,'%-5.0d\n',-9999);

This place the numeric values starting in column 15 and removes the trailing zeros from the last two values.

Updates
31 Aug 2007

bug fixed in conversion to raster portion of program

31 Mar 2009

changed description, help and example

Contact us