function [nodes, elements, edges]  = readmesh(name)

elements = readele(strcat(name,'.ele'));
nodes = readnode(strcat(name,'.node'));
edges = readedge(strcat(name,'.edge'));

    function ele = readele(fname)
      fid = fopen(fname, 'rt');
  
      if (fid < 0)
        error('Could not open the file.');
      end;

      % read the first line of the elements file
      first_line  = fscanf(fid, '%d', 3);
      size = first_line(1);
      vert = first_line(2);
      att = first_line(3);
    
      ele = zeros(size, vert, 'int16');
    
      % Again, we use a cycle instead of matlab specific 
      %   functions like textscan, so that you can easily
      %   port to another programming language if needed.
  
      for i=1:size
          % read the first number on the current line
          %   (for our files this is just the element number)
          j=fscanf(fid, '%d', 1); 
          if (j~=i)
              error('Invalid file format.');
          end
      
          % read the three (vert) numbers defining the
          %  vertices of the current element
          ele(i,:) = fscanf(fid, '%d', vert)';

          % finally read the attributes (which we currently discard)
          fscanf(fid, '%d', att);
      end
  
      fclose(fid);
    end

    function node = readnode(fname)
      fid = fopen(fname, 'rt');
  
      if (fid < 0)
        error('Could not open the file.');
      end;

      % read the first line of the nodes file
      first_line  = fscanf(fid, '%d', 4);
      size = first_line(1);
      dim = first_line(2);
      att = first_line(3);
      bdr = first_line(4);
    
      node = zeros(size, dim+bdr);
    
      % Again, we use a cycle instead of matlab specific 
      %   functions like textscan, so that you can easily
      %   port to another programming language if needed.
  
      for i=1:size
          % read the first number on the current line
          %   (for our files this is just the node number)
          j=fscanf(fid, '%d', 1); 
          if (j~=i)
              error('Invalid file format.');
          end
      
          % read the two (dim) real numbers defining the
          %  coordinates of the current node
          node(i,1:dim) = fscanf(fid, '%f', dim)';

          % read the attributes (which we currently discard)
          fscanf(fid, '%d', att);
          
          % and the boundary marker
          if (bdr~=0)
              node(i,dim+1:dim+bdr) = fscanf(fid, '%d', bdr)';
          end
      end
  
      fclose(fid);
    end

    function edge = readedge(fname)
      fid = fopen(fname, 'rt');
  
      if (fid < 0)
        error('Could not open the file.');
      end;

      % read the first line of the nodes file
      first_line  = fscanf(fid, '%d', 2);
      size = first_line(1);
      bdr = first_line(2);
    
      edge = zeros(size, 2+bdr);
    
      % Again, we use a cycle instead of matlab specific 
      %   functions like textscan, so that you can easily
      %   port to another programming language if needed.
  
      for i=1:size
          % read the first number on the current line
          %   (for our files this is just the edge number)
          j=fscanf(fid, '%d', 1); 
          if (j~=i)
              error('Invalid file format.');
          end
      
          % read the two integers defining the end-points
          %  coordinates of the current edge
          edge(i,1:2) = fscanf(fid, '%d', 2)';

          % and the boundary marker
          if (bdr~=0)
              edge(i,3:2+bdr) = fscanf(fid, '%d', bdr)';
          end
      end
  
      fclose(fid);
    end



end
