%To read some sequences of easting & northing coordinates. In furrytran, using free-format I/O, the following would do:
%      READ(in,*) GN,ORDER,NAME(1:ORDER)			!Grabs the heading line.
%      READ(in,*) (EASTING(I),NORTHING(I),I = 1,GN)	!Grabs the positions.
%That would read one sequence of positions. To read a horde, use higher order arrays and a loop...
%      PIECE = 0		!No pieces in hand.
%   10 READ(in,*,END = 20) GN,ORDER,NAME(PIECE + 1,1:ORDER)	!The header for a new piece, or, end-of-file.
%      PIECE = PIECE + 1	!Get here, another heading has been found.
%      COUNT(PIECE) = GN	!Record its promise of the number of points to follow.
%      READ(in,*) (EASTING(PIECE,I),NORTHING(PIECE,I),I = 1,GN)	!Obtain the promised positions, presuming proper precedence.
%      GO TO 10			!And try for yet another piece.
%   20 CLOSE(in)		!No more pieces.
%The data file starts as follows:
%   6934,      1,Island South
%5978910,2597810
%5976630,2596875
%etc, for 6934 grid positions.
%But in matlab, some struggle ensues.
stuff = fopen('NZoutlineNZMG.txt','r');
rec = 0;
piece = 0;
clear Mark;
damn = fgetl(stuff);		%Idiot pump priming.
while ischar(damn)		%Why not while fgetl(stuff) ?
 rec = rec + 1;
 piece = piece + 1;
 [n,order] = strread(damn,'%u %u',1,'delimiter',',');	%This is really messy.
 if n > 100000 || abs(order) > 6, disp('Urk!'); return; end;	%Probably a miscount for n.
 clear c name; c = strfind(damn,',');			%And this is worse.
 c = [c(2:end) size(damn,2) + 1];	%Finger the end-of-fields.
 for j = 1:order			%And plough them one by one. Pathetic.
  name(j) = strread(damn(c(j) + 1:c(j + 1) - 1),'%s',1,'delimiter',',');	%= damn(c(1:end - 1) + 1:c(2:end) - 1);
 end;
 damn = ['Record ',int2str(rec),': Piece ',int2str(piece),' order ',int2str(order),' points ',int2str(n),' name '];
 for j = 1:order				%Damnit, "name" is an array of strings.
  damn = [damn,' ',char(strcat(name(j)))];	%Why can't I just write them one after the other?
 end;						%Rather than this floundering.
 disp(damn);					%Take that.
 Mark{piece}.Name = name;
 Mark{piece}.Order = order;
 Mark{piece}.Points = n;
 Mark{piece}.Easting = zeros(1,n);
 Mark{piece}.Northing = zeros(1,n);
 for i = 1:n
  damn = fgetl(stuff);
  if ischar(damn)
   rec = rec + 1;
   [GE GN] = strread(damn,'%f %f',1,'delimiter',',');	%At last, grab the Easting and Northing.
   if GE < 5000000		%NZMG is arranged so that all eastings in NZ are less than 500000.
     Mark{piece}.Easting(i) = GE; Mark{piece}.Northing(i) = GN;
    else			%They might be specified the wrong way around.
     Mark{piece}.Easting(i) = GN; Mark{piece}.Northing(i) = GE;
   end;
  end;
 end;				%On to the next position.
 damn = fgetl(stuff);		%Perhaps another piece follows.
end;			%Back to deal with the next piece.
disp([int2str(rec),' records, ',int2str(piece),' pieces.']);
fclose(stuff);

figure(1); clf; hold on; axis equal;
colour = cellstr(strvcat('k','b','r','c','m','y','k'));
for i = 1:piece
 it = fix(Mark{i}.Order);
 plot([Mark{i}.Easting Mark{i}.Easting(1)],[Mark{i}.Northing Mark{i}.Northing(1)],colour{it});
end;
clear i it j n c rec damn piece stuff order name
