Index: /trunk/src/check_field_structure.m
===================================================================
--- /trunk/src/check_field_structure.m	(revision 139)
+++ /trunk/src/check_field_structure.m	(revision 140)
@@ -32,26 +32,15 @@
     nbfield=numel(Data.ListVarName); 
 else
-    errormsg='input field does not contain the list of variables .ListVarNames';
+    errormsg='input field does not contain the cell array of variable names .ListVarNames';
     return
 end
 %check dimension names
-% definition by VarDimIndex (obsolete)
-% if ~isfield(Data,'VarDimName') && isfield(Data,'VarDimIndex') && isfield(Data,'ListDimName')%old convention
-%     for ivar=1:nbfield
-%         DimCell=Data.VarDimIndex{ivar};
-%         if isnumeric(DimCell)
-%             if DimCell <= numel(Data.ListDimName)
-%                 Data.VarDimName{ivar}=Data.ListDimName(DimCell);
-%             else                      
-%                 errormsg='dimension names not defined';
-%                 return 
-%             end
-%         else 
-%             errormsg='unrecognized format for .VarDimIndex';
-%         end
-%     end
-% end
-if ~(isfield(Data,'VarDimName') && iscell(Data.VarDimName))
-    errormsg='input field does not contain the list of dimensions .VarDimName';
+if (isfield(Data,'VarDimName') && iscell(Data.VarDimName))
+    if  numel(Data.VarDimName)~=nbfield
+       errormsg=' .ListVarName and .VarDimName have different lengths';
+        return
+    end
+else
+    errormsg='input field does not contain the  cell array of dimension names .VarDimName';
     return
 end
@@ -59,91 +48,72 @@
     Data=rmfield(Data,'DimValue');
 end
-if isfield(Data,'VarDimName') && iscell(Data.VarDimName)
-    nbdim=0;
-    if numel(Data.VarDimName)==nbfield
-        for ivar=1:nbfield
-            VarName=Data.ListVarName{ivar};
-            if ~isfield(Data,VarName)
-                errormsg=['the listed variable ' VarName ' is not found'];
+nbdim=0;
+Data.ListDimName={};
+for ivar=1:nbfield
+    VarName=Data.ListVarName{ivar};
+    if ~isfield(Data,VarName)
+        errormsg=['the listed variable ' VarName ' is not found'];
+        return
+    end
+    eval(['sizvar=size(Data.' VarName ');'])% sizvar = dimension of variable
+    DimCell=Data.VarDimName{ivar};
+    if ischar(DimCell)
+        DimCell={DimCell};%case of a single dimension name, defined by a string
+    elseif ~iscell(DimCell)
+        errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
+        return
+    end
+    nbcoord=numel(sizvar);%nbre of coordiantes for variable named VarName
+    if numel(DimCell)==0
+        errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
+        return
+    elseif numel(DimCell)==1% one dimension declared
+        if nbcoord==2
+            if sizvar(1)==1
+                nbcoord=1;
+                sizvar(1)=sizvar(2);
+            elseif sizvar(2)==1
+                nbcoord=1;
+            else
+                errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
                 return
-            else             
-                eval(['sizvar=size(Data.' VarName ');'])% sizvar = dimension of variable
-                DimCell=Data.VarDimName{ivar};
-                if ischar(DimCell)
-                    DimCell={DimCell};
-                elseif ~iscell(DimCell)
-                    errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
+            end
+        else
+            errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
+            return
+        end
+    else
+        if numel(DimCell)>nbcoord
+            DimCell=DimCell(end-nbcoord+1:end);%first singleton diemnsions omitted,
+        elseif nbcoord > numel(DimCell)
+            errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
+            return
+        end
+    end
+    DimIndex=[];
+    for idim=1:nbcoord %loop on the coordinates of variable #ivar
+        DimName=DimCell{idim};
+        iprev=find(strcmp(DimName,Data.ListDimName),1);%look for dimension name DimName in the current list
+        if isempty(iprev)% append the dimension name to the current list
+            nbdim=nbdim+1;
+            if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing)
+                RangeTest(nbdim)=1; %to be updated for a later variable  
+            end
+            Data.DimValue(nbdim)=sizvar(idim);
+            Data.ListDimName{nbdim}=DimName;
+            DimIndex=[DimIndex nbdim];
+        else % DimName is detected in the current list of dimension names
+            if ~isequal(Data.DimValue(iprev),sizvar(idim))
+                if isequal(Data.DimValue(iprev),2)&& RangeTest(iprev)  % the dimension has been already detected as a range [min max]
+                    Data.DimValue(iprev)=sizvar(idim); %update with actual value
+                else
+                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
                     return
                 end
-                nbcoord=numel(sizvar);
-                if numel(DimCell)==0
-                    errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
-                    return
-                elseif numel(DimCell)==1% one dimension declared
-                    if nbcoord==2 
-                        if sizvar(1)==1 
-                            nbcoord=1;
-                            sizvar(1)=sizvar(2);
-                        elseif sizvar(2)==1
-                            nbcoord=1;
-                        else
-                            errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
-                            return
-                        end
-                    else
-                          errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
-                          return      
-                    end  
-                else
-                    if numel(DimCell)>nbcoord
-                        DimCell=DimCell(end-nbcoord+1:end);%first singleton diemnsions omitted, 
-                    elseif nbcoord > numel(DimCell)
-                        errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
-                        return
-                    end
-                end
-                DimIndex=[];
-                for idim=1:nbcoord %loop on the coordinates of variable #ivar   
-                    DimName=DimCell{idim};
-                    testprev=0;
-                    for iprev=1:nbdim %check previously listed dimension names
-                        if strcmp(Data.ListDimName{iprev},DimName)
-                           if ~isequal(Data.DimValue(iprev),sizvar(idim))
-                               if isequal(Data.DimValue(iprev),0)  % the dimension has been already detected as a range [min max]
-                                   Data.DimValue(idim)=sizvar(idim); %update with actual value 
-                               elseif sizvar(idim)==2 && strcmp(DimName,VarName)
-                                    %case of a regularly spaced coordinate defined by the first and last values: dimension will be determined later                          
-                               else
-                                   errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
-                                   return 
-                               end
-                           end
-                           DimIndex=[DimIndex iprev];
-                           testprev=1;
-                           break
-                        end
-                    end
-                    if ~testprev % a new dimension is appended to the list
-                        nbdim=nbdim+1;
-                        if sizvar(idim)==2 && strcmp(DimName,VarName)
-                            Data.DimValue(nbdim)=0; %to be updated for a later variable
-%                             Data.VarType{ivar}='range';
-                        else
-                            Data.DimValue(nbdim)=sizvar(idim);
-                        end
-                        Data.ListDimName{nbdim}=DimName;
-                        DimIndex=[DimIndex nbdim];
-                    end
-                end
-                Data.VarDimIndex{ivar}=DimIndex;
             end
-        end                               
-    else
-        errormsg=' .ListVarNames and .VarDimName have different lengths';
-        return
+            DimIndex=[DimIndex iprev];
+        end
     end
-else
-    errormsg='input field does not contain the cell of dimension names .VarDimName for variables';
-    return
+    Data.VarDimIndex{ivar}=DimIndex;
 end
 DataOut=Data;
Index: /trunk/src/nc2struct.m
===================================================================
--- /trunk/src/nc2struct.m	(revision 139)
+++ /trunk/src/nc2struct.m	(revision 140)
@@ -7,6 +7,6 @@
 % OUTPUT:
 %  Data: structure containing all the information of the netcdf file (or netcdf object)
-%           with fields:
-%         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
+%           with (optional)fields:
+%                    .ListGlobalAttribute: cell listing the names of the global attributes
 %                    .Att_1,Att_2... : values of the global attributes
 %                    .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} ) 
@@ -15,15 +15,17 @@
 %                    .ListDimName=list of dimension (added information, not requested for field description)
 %                    .DimValue= vlalues of dimensions (added information, not requested for field description)
-%                    .VarDimIndex= list of dimension indices (added information,
-%
+%  var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else.
+%            var_detect=[] in the absence of input cell array 
+%  ichoice: index of the selected line in the case of multiple choice 
+%        (cell array of varible names with multiple lines) , =[] by default 
 %INPUT:
-%     nc:      name of a netcdf file (char string) or netcdf object   
-%    additional arguments:
-%         -in the absence of other arguments, all the fields are read
-%         -a cell array, ListVarName, of  char strings {'VarName1', 'VarName2',...} ) 
+%  nc:  name of a netcdf file (char string) or netcdf object   
+%  additional arguments:
+%       -no additional arguments: all the variables of the netcdf fiel are read.
+%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} ) 
 %         if ListVarName=[] or {}, no variables is read (only global attributes)
-%         if ListVarName is absent, or = '*', ALL the variables are read. 
-%        if ListVarName is a cell array with n lines, the set of variables
-%                        will be sought by order of priority in the list, while output names will be set by the first line
+%         if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read. 
+%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority
+%                  in the list, while output names will be set by the first line
 %        - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
 % 
@@ -52,9 +54,10 @@
 
 if ~isequal(hhh,'')
-    %default output
-    Data=[];
-    var_detect=[];
+    %% default output
+    Data=[];%default
+    var_detect=[];%default
     ichoice=[];%default
-    %open the netcdf file for reading
+    
+    %% open the netcdf file for reading
     if ischar(nc) 
         if exist(nc,'file')
@@ -68,5 +71,6 @@
         testfile=0;
     end
-    % short reading of global attributes
+    
+    %% short reading opion for global attributes only, if the first argument is 'ListGlobalAttribute'
     if isequal(varargin{1},'ListGlobalAttribute')
         for ilist=2:numel(varargin)
@@ -82,9 +86,9 @@
     end
 
-    % reading of variables, including attributes
-    ListVarName=varargin{1};  
-    [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, attributes
-    
-    %  -------- read global attributes (constants)-----------
+    %% full reading: get the nbre of dimensions, variables, global attributes
+    ListVarName=varargin{1}; 
+    [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdf file
+    
+    %%  -------- read all global attributes (constants)-----------
     att_key={};%default
     iatt_g=0;
@@ -92,6 +96,6 @@
     for iatt=1:ngatts
         keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1);
-        indstr1=regexp(keystr,'\\');%detect '\\'
-        indstr2=regexp(keystr,'\.');%detect '\.'
+        indstr1=regexp(keystr,'\\','once');%detect '\\'
+        indstr2=regexp(keystr,'\.','once');%detect '\.'
         if isempty(indstr1) && isempty(indstr2)
            valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr);
@@ -117,100 +121,58 @@
     Data.ListGlobalAttribute=att_key;
 
-    %  -------- read dimensions -----------
-    ListDimName={};
+    %%  -------- read dimension names-----------
+    ListDimNameNetcdf={};
     dim_value=[];
-    for idim=1:ndims%length(dim_read);
-        [ListDimName{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);
-    end
-    if ~isempty(ListDimName) %&& ~isempty(dim_value)
-%         Data.DimValue=dim_value;
-%         DimIndices=[1:ndims]; %index of the dimension in the netcdf file
-        flag_used=zeros(1,ndims);%initialize test of used dimensions
+    for idim=1:ndims %loop on the dimensions of the netcdf file
+        [ListDimNameNetcdf{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);%get name and value of each dimension
+    end
+    if ~isempty(ListDimNameNetcdf) 
+        flag_used=zeros(1,ndims);%initialize the flag indicating the selected dimensions in the list (0=unused)
     end
  
-    %  -------- read variables -----------
-    var_read={}; %default
+    %%  -------- read names of variables -----------
+    ListVarNameNetcdf={}; %default
     dimids={};
     nbatt=[];
-    for ivar=1:nvars
-        [var_read{ivar},xtype,dimids{ivar},nbatt(ivar)] = netcdf.inqVar(nc,ivar-1); 
+    for ncvar=1:nvars %loop on the variables of the netcdf file
+        %get name, type, dimensions and attribute numbers of each variable 
+        [ListVarNameNetcdf{ncvar},xtype,dimids{ncvar},nbatt(ncvar)] = netcdf.inqVar(nc,ncvar-1);
     end  
-    var_index=1:nvars; %default set of variable indices in the netcdf file
     testmulti=0;
-    OutputList=[];
-    %select input variables, if requested by the input ListVarName
-    if ~(isequal(ListVarName,'*')||isempty(ListVarName))
+    if isequal(ListVarName,'*')||isempty(ListVarName)
+        var_index=1:nvars; %all the variables are selected in the netcdf file 
+        Data.ListVarName=ListVarNameNetcdf;
+    else   %select input variables, if requested by the input ListVarName
         sizvar=size(ListVarName);
-        testmulti=(sizvar(1)>1);
+        testmulti=(sizvar(1)>1);%test for multiple choice of variable ranked by order of priority
         var_index=zeros(1,sizvar(2));%default
-        if testmulti
-            OutputList=ListVarName(1,:);
-            testend=0;
+        if testmulti %multiple choice of variable ranked by order of priority
             for iline=1:sizvar(1)
-                if testend
-                    break
+                search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of netcdf variables
+                if ~isempty(search_index)
+                    break % go to the next line
                 end
-          %      var_index=zeros(size(ListVarName));%default
-                for ivar=1:sizvar(2)
-                    if ~isempty(ListVarName{iline,ivar})
-                         for ilist=1:nvars
-                            if isequal(var_read{ilist},ListVarName{iline,ivar})
-                                var_index(ivar)=ilist;
-     %                          var_detect(ivar)=1;
-                            break
-                            end
-                         end
-                         if ivar==1
-                            if var_index(ivar)==0
-                                break%go to next line if the first nc variable is not found
-                            else
-                                testend=1; %this line will be read
-                                ichoice=iline-1; %selectedline number in the list of input names of variables
-                            end
-                         end
-                    end
-                end
-            end
-        else   %single list of input variables
-            for ivar=1:sizvar(2)
-                for ilist=1:nvars
-                    if isequal(var_read{ilist},ListVarName{ivar})
-                        var_index(ivar)=ilist;
-                        var_detect(ivar)=1;
-                        break
-                    end
-                end
-            end
-        end
-        list_index=find(var_index);
-        if ~isempty(list_index)
-            if testmulti
-                OutputList=OutputList(list_index);
-            end
-            var_index=var_index(list_index);
-            var_detect=(var_index~=0);
-            var_read=var_read(var_index);         
-        end
-    end
-       
-    %select variable attributes and associate dimensions
-%     var_dim_index=[]; %default
-    Data.ListVarName={};%default
-    VarDimIndex={};%default
-    for ivar=1:length(var_read)
-        if testmulti
-            Data.ListVarName{ivar}=OutputList{ivar};%new name given by ListVarName(1,:)
+            end
+            ichoice=iline-1;%selected line number in the list of input names of variables
         else
-            Data.ListVarName{ivar}=var_read{ivar};%name of the variable
-        end
-        var_dim=dimids{var_index(ivar)}+1; %dimension indices used by the variable
-        flag_used(var_dim)=ones(size(var_dim));%flag_used =1 for the indices of used dimensions
-        VarDimIndex{ivar}=var_dim;
-
-        %variable attributes
-        if ivar==1
-            Data.VarAttribute={};%initialisation of the list of variable attributes
-        end
-        %variable attributes
+            iline=1;
+        end
+        for ivar=1:sizvar(2)
+            search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of netcdf file
+            if ~isempty(search_index)
+                var_index(ivar)=search_index;%index of the netcdf list corresponding to the input list index ivar
+            end
+        end
+        var_detect=(var_index~=0);%=1 for detected variables          
+        list_index=find(var_index);% indices in the input list corresponding to a detected variable
+        var_index=var_index(list_index);% netcdf variable indices corresponding to the output list of read variable
+        Data.ListVarName=ListVarName(1,list_index);%the first line of ListVarName sets the output names of the variables
+    end
+     
+  %% get the dimensions and attributes associated to  variables
+    for ivar=1:length(var_index)
+        var_dim{ivar}=dimids{var_index(ivar)}+1; %netcdf dimension indices used by the variable #ivar
+        Data.VarDimName{ivar}=ListDimNameNetcdf(var_dim{ivar});
+        flag_used(var_dim{ivar})=ones(size(var_dim{ivar}));%flag_used =1 for the indices of used dimensions
         for iatt=1:nbatt(var_index(ivar))
             attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1);
@@ -226,32 +188,26 @@
     end
 
-    %select the used dimensions
-    if ~isempty(var_read) 
-        dim_index=find(flag_used);
-        old2new=cumsum(flag_used); 
-        ListDimName=ListDimName(dim_index); 
-        dim_value=dim_value(dim_index);
-    end
-    for ivar=1:length(var_read)
-        Data.VarDimName{ivar}=ListDimName(old2new(VarDimIndex{ivar}));
-    end
-    Data.ListDimName=ListDimName;
-    Data.DimValue=dim_value;
-    Data.VarDimIndex= VarDimIndex;
-    %variable values
+    %% select the dimensions used for the set of input variables
+    if ~isempty(var_index)      
+        dim_index=find(flag_used);%list of netcdf dimensions indices corresponding to used dimensions 
+        Data.ListDimName=ListDimNameNetcdf(dim_index); 
+        Data.DimValue=dim_value(dim_index);
+    end
+    
+    %% get the values of the input variables
     if  ~isempty(ListVarName)
-        for ivar=1:length(Data.ListVarName)
+        for ivar=1:length(var_index)
             VarName=Data.ListVarName{ivar};
-            indstr=regexp(VarName,'-');%detect '-'
-            if ~isempty(indstr)
-                VarName(indstr)=[];
-            end
+            VarName=regexprep(VarName,'-',''); %suppress '-' if it exists in the netcdf variable name
             eval(['Data.' VarName '=double(netcdf.getVar(nc,var_index(ivar)-1));'])%read the variable data
         end
     end
-    %  -------- close fle-----------
+    
+    %%  -------- close fle-----------
     if testfile==1
         netcdf.close(nc) 
     end
+    
+%% old netcdf library 
 else
     [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
Index: /trunk/src/nc2struct_toolbox.m
===================================================================
--- /trunk/src/nc2struct_toolbox.m	(revision 139)
+++ /trunk/src/nc2struct_toolbox.m	(revision 140)
@@ -1,29 +1,32 @@
 %'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY
+% it reads all the global attributes and all variables, or a selected list.
+% The corresponding dimensions and variable attributes are then extracted
 %----------------------------------------------------------------------
-% function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListField)
+% function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin)
 %
 % OUTPUT:
 %  Data: structure containing all the information of the netcdf file (or netcdf object)
-%           with fields:
-%    .ListGlobalAttribute: cell listing the names of the global attributes
-%        .Att_1,Att_2... : values of the global attributes
-%            .ListDimName: cell listing the names of the array dimensions
-%               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
-%            .ListVarName: cell listing the names of the variables
-%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
-%            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
-%           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
-%        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
-%  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
-%  ichoice: = line 
-%
+%           with (optional)fields:
+%                    .ListGlobalAttribute: cell listing the names of the global attributes
+%                    .Att_1,Att_2... : values of the global attributes
+%                    .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} ) 
+%                    .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)                         
+%                    .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
+%                    .ListDimName=list of dimension (added information, not requested for field description)
+%                    .DimValue= vlalues of dimensions (added information, not requested for field description)
+%  var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else.
+%            var_detect=[] in the absence of input cell array 
+%  ichoice: index of the selected line in the case of multiple choice 
+%        (cell array of varible names with multiple lines) , =[] by default 
 %INPUT:
-%     nc:      name of a netcdf file (char string) or netcdf object   
-% ListField: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} ) 
-%         if ListField is absent or ='*', ALL the attributes and variables are read.  %      
-%        if  ListField='ListGlobalAttribute', followed by the arguments 'name1', name2'..., only thes global attributes will be read (short option)
-%        if  ListField=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)
-%        if ListField is a cell array with n lines, the set of variables
-%                        will be sought by order of priority in the list, while output names will be set by the first line
+%  nc:  name of a netcdf file (char string) or netcdf object   
+%  additional arguments:
+%       -no additional arguments: all the variables of the netcdf fiel are read.
+%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} ) 
+%         if ListVarName=[] or {}, no variables is read (only global attributes)
+%         if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read. 
+%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority
+%                  in the list, while output names will be set by the first line
+%        - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
 % 
 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
@@ -203,5 +206,5 @@
         for idim=1:length(dim_name)% find the index of the current dimension in the list of dimensions
             if isequal(dim_name{idim},var_dim_name)
-                Data.VarDimIndex{ivar}(ivardim)=idim;
+                VarDimIndex{ivar}(ivardim)=idim;
                 used(idim)=1;
                 break
@@ -240,6 +243,6 @@
 end
 for ivar=1:length(var_read)
-    Data.VarDimIndex{ivar}=(old2new(Data.VarDimIndex{ivar}));
-    Data.VarDimName{ivar}=(Data.ListDimName(Data.VarDimIndex{ivar}));
+    VarDimIndex{ivar}=(old2new(VarDimIndex{ivar}));
+    Data.VarDimName{ivar}=(Data.ListDimName(VarDimIndex{ivar}));
 end
 %variable values
Index: /trunk/src/read_civxdata.m
===================================================================
--- /trunk/src/read_civxdata.m	(revision 139)
+++ /trunk/src/read_civxdata.m	(revision 140)
@@ -71,6 +71,6 @@
     return % error in file reading
 end
-if isequal(vardetect,0)
-     Field.Txt=[reshape(FieldNames,1,numel(FieldNames)) ' not accessible in ' filename '/' VelType];
+if vardetect(1)==0
+     Field.Txt=[ 'requested field not available in ' filename '/' VelType];
      return
 end
@@ -137,10 +137,10 @@
 % rescale to pixel coordiantes
 if isfield(Field,'pixcmx')
-Field.pixcmx=double(Field.pixcmx); 
-Field.pixcmy=double(Field.pixcmy);
-Field.U=Field.U*Field.pixcmx;
-Field.V=Field.V*Field.pixcmy;
-Field.X=Field.X*Field.pixcmx;
-Field.Y=Field.Y*Field.pixcmy; 
+    Field.pixcmx=double(Field.pixcmx);
+    Field.pixcmy=double(Field.pixcmy);
+    Field.U=Field.U*Field.pixcmx;
+    Field.V=Field.V*Field.pixcmy;
+    Field.X=Field.X*Field.pixcmx;
+    Field.Y=Field.Y*Field.pixcmy;
 end
 if ~isequal(Field.dt,0)
Index: /trunk/src/struct2nc.m
===================================================================
--- /trunk/src/struct2nc.m	(revision 139)
+++ /trunk/src/struct2nc.m	(revision 140)
@@ -51,5 +51,9 @@
         return
     end
-    [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure
+    [Data,errormsg]=check_field_structure(Data)%check the validity of the input field structure
+    if ~isempty(errormsg)
+        errormsg=['invalid input structure:' errormsg];
+        return
+    end
     ListVarName=Data.ListVarName;
     nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname   
@@ -110,9 +114,9 @@
             eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable 
             VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
-            siz=size(VarVal);
             VarDimName=Data.VarDimName{ivar};
             if ischar(VarDimName)
                 VarDimName={VarDimName};
             end
+            siz=size(VarVal);
             testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
             testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));%matlab vector
