1  % script to transform .mat files into netcdf


2  DataFolder=pwd; %=current working directory: to replace by path to data


3  fileinput=uigetfile_uvmat('pick an input .mat file',DataFolder,'.mat');% pick a .mat file by the browser


4  ncfile=regexprep(fileinput,'.mat$','.nc');% replace extension .mat by .nc


5  Data=load(fileinput)% load data from .mat file contains all variables


6  ListFields=fieldnames(Data);% list of all variable names


7 


8  % Example of variable selection: look for the variable with higher dimensions


9  Npy=zeros(1,numel(ListFields));


10  Npx=zeros(1,numel(ListFields));


11  for ilist =1:numel(ListFields)


12  [Npy(ilist),Npx(ilist)]=size(Data.(ListFields{ilist}));


13  end


14  [tild,ilist]=max(Npy.*Npx);


15  Data.coord_x=1:Npx(ilist);% coordinate variable


16  Data.coord_y=1:Npy(ilist);


17  Data.ListVarName={'coord_x','coord_y',ListFields{ilist}};


18  Data.VarDimName={'coord_x','coord_y',{'coord_y','coord_x'}};


19 


20  errormsg=struct2nc(ncfile,Data); % write the netcdf file


21  if isempty(errormsg)


22  disp([ncfile ' written'])


23  else


24  disp(errormsg)


25  end


26  Dataread=nc2struct(ncfile)% check the netcdf file


27 

