source: trunk/src/script_mat2nc.m @ 1114

Last change on this file since 1114 was 890, checked in by sommeria, 10 years ago

bugs solved for reading general netcdf files

File size: 1.1 KB
Line 
1% script to transform .mat files into netcdf
2DataFolder=pwd; %=current working directory: to replace by path to data
3fileinput=uigetfile_uvmat('pick an input .mat file',DataFolder,'.mat');% pick a .mat file by the browser
4ncfile=regexprep(fileinput,'.mat$','.nc');% replace extension .mat by .nc
5Data=load(fileinput)% load data from .mat file contains all variables
6ListFields=fieldnames(Data);% list of  all variable names
7
8% Example of variable selection: look for the variable with higher dimensions
9Npy=zeros(1,numel(ListFields));
10Npx=zeros(1,numel(ListFields));
11for ilist =1:numel(ListFields)
12    [Npy(ilist),Npx(ilist)]=size(Data.(ListFields{ilist}));
13end
14[tild,ilist]=max(Npy.*Npx);
15Data.coord_x=1:Npx(ilist);% coordinate variable
16Data.coord_y=1:Npy(ilist);
17Data.ListVarName={'coord_x','coord_y',ListFields{ilist}};
18Data.VarDimName={'coord_x','coord_y',{'coord_y','coord_x'}};
19
20errormsg=struct2nc(ncfile,Data); % write the netcdf file
21if isempty(errormsg)
22    disp([ncfile ' written'])
23else
24    disp(errormsg)
25end
26Dataread=nc2struct(ncfile)% check the netcdf file
27
Note: See TracBrowser for help on using the repository browser.