function fields=CMUloadfields(filename)
% function fields=CMUloadfields(filename)
% loads all fields in filename into structure fields.e.x fields fields.e.y
% fields.e.z
% fields.h.x fields fields.h.y
% fields.h.z
% fields.coor.x fields fields.coor.y (coordinates)
% fields.n (ref index)
% fields.mat (material number)
% all are square matrices 
% filename has to be saved using winfield's all fields option, into an
% cmuaf file
% data structure is 
% resolution
%     followed by (resolution+1)^2 lines of
% x,y,n,mat,exr,exi,eyr,eyi,ezr,ezi,hxi,hxr,hyr,hyi,hzr,hzi

numbers=load(filename);
resolution=sqrt(size(numbers,1));
    fields.x=numbers(:,1);
    fields.y=numbers(:,2);
    fields.n=numbers(:,3)+1i*numbers(:,4);
    fields.mat=numbers(:,5);
    fields.ex=numbers(:,6)+1i*numbers(:,7);
    fields.ey=numbers(:,8)+1i*numbers(:,9);
    fields.ez=numbers(:,10)+1i*numbers(:,11);
    fields.hx=numbers(:,12)+1i*numbers(:,13);
    fields.hy=numbers(:,14)+1i*numbers(:,15);
    fields.hz=numbers(:,16)+1i*numbers(:,17);    

    fields.x=reshape(fields.x,resolution,resolution);
fields.y=reshape(fields.y,resolution,resolution);
fields.n=reshape(fields.n,resolution,resolution);
fields.mat=reshape(fields.mat,resolution,resolution);
fields.ex=reshape(fields.ex,resolution,resolution);
fields.ey=reshape(fields.ey,resolution,resolution);
fields.ez=reshape(fields.ez,resolution,resolution);
fields.hx=reshape(fields.hx,resolution,resolution);
fields.hy=reshape(fields.hy,resolution,resolution);
fields.hz=reshape(fields.hz,resolution,resolution);
end
