function writestruct2asc(AscStruct, fName)

fid      = fopen(fName,'w');

%write header, example
%     ncols    500
%     nrows    625
%     xllcorner  260000.00
%     yllcorner  575000.00
%     cellsize      20.00
%     nodata_value  -9999.000

fprintf(fid,'ncols %i \n',AscStruct.ncols);
fprintf(fid,'nrows %i \n',AscStruct.nrows);
fprintf(fid,['xllcorner %f \n'],AscStruct.xllcorner);
fprintf(fid,['yllcorner %f \n'],AscStruct.yllcorner);
fprintf(fid,['cellsize %f \n'],AscStruct.cellsize);
fprintf(fid,['nodata_value %f \n'],AscStruct.nodata_value);

%write data
AscStruct.(AscStruct.varname)(isnan(AscStruct.(AscStruct.varname))) = AscStruct.nodata_value;

div = 20;
step = ceil(AscStruct.ncols/div);
for r=1:AscStruct.nrows
    n1 = 1 - div;
    while (true)
        n1 = n1 + div;
        n2 = n1 + div - 1; 
        if n2 >= AscStruct.ncols
            n2 = AscStruct.ncols;
            s=[sprintf(' %f ',AscStruct.(AscStruct.varname)(r,n1:n2))];
            fprintf(fid,'%s\n',s);
            break
        else
            s=[sprintf(' %f ',AscStruct.(AscStruct.varname)(r,n1:n2))];
            fprintf(fid,'%s\n',s);
        end
    end
end    
    
fclose(fid);
