source: trunk/src/mat2struct.m @ 1160

Last change on this file since 1160 was 1095, checked in by sommeria, 4 years ago

reading mat files added, +-fixed,OpenDAP improved

File size: 1.5 KB
Line 
1%'mat2struct': read a matlab file .mat as a structure similar to the output
2%of nc2struct
3%----------------------------------------------------------------------
4function Field=mat2struct(filename,ListFieldName)
5
6Field=load(filename);%case of .mat data file
7if ~exist('ListFieldName','var')
8    ListFieldName=fieldnames(Field);
9end
10ivar=0;
11Field.DimValue=[];
12Field.ListGlobalAttribute={};
13Field.ListVarName={};
14ndim=0;
15for ilist=1:numel(ListFieldName)
16    if isnumeric(Field.(ListFieldName{ilist})) && numel(Field.(ListFieldName{ilist}))>=2
17        ivar=ivar+1;
18        Field.ListVarName=[Field.ListVarName ListFieldName{ilist}];
19        ndim_var=0;
20        for idim=1:ndims(Field.(ListFieldName{ilist}))
21            sizevar=size(Field.(ListFieldName{ilist}),idim);
22            if sizevar>1% avoid singleton dimensions
23                ndim_var=ndim_var+1;
24                prevdim_index=find(Field.DimValue==sizevar);%look for the same value of dimension
25                if isempty(prevdim_index)% new dimension detected
26                    ndim=ndim+1;
27                    Field.DimValue(ndim)=sizevar;
28                    Field.ListDimName{ndim}=num2str(sizevar);
29                    Field.VarDimName{ivar}{ndim_var}=Field.ListDimName{ndim};
30                else
31                    Field.VarDimName{ivar}{ndim_var}=Field.ListDimName{prevdim_index};
32                end
33            end
34        end
35        Field.VarType(ivar)=5;%for double variable
36    else
37        Field.ListGlobalAttribute=[Field.ListGlobalAttribute ListFieldName{ilist}];
38    end
39end
Note: See TracBrowser for help on using the repository browser.