Changeset 227 for trunk/src/nc2struct.m
- Timestamp:
- Mar 31, 2011, 1:42:51 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/nc2struct.m
r188 r227 65 65 nc=netcdf.open(nc,'NC_NOWRITE'); 66 66 testfile=1; 67 catch errormsg68 Data.Txt=['ERROR opening ' nc ': ' errormsg.identifier];67 catch ME 68 Data.Txt=['ERROR opening ' nc ': ' ME.message]; 69 69 return 70 70 end … … 77 77 end 78 78 79 %% short reading op ion for global attributes only, if the first argument is 'ListGlobalAttribute'79 %% short reading option for global attributes only, if the first argument is 'ListGlobalAttribute' 80 80 if isequal(varargin{1},'ListGlobalAttribute') 81 81 for ilist=2:numel(varargin) 82 valuestr=[];%default 82 83 try 83 84 valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),varargin{ilist}); 84 catch 85 valuestr=[]; 85 catch ME 86 86 end 87 87 eval(['Data.' varargin{ilist} '=valuestr;']) … … 97 97 %% -------- read all global attributes (constants)----------- 98 98 att_key={};%default 99 iatt_g=0;99 % iatt_g=0; 100 100 Data.ListGlobalAttribute={};%default 101 101 for iatt=1:ngatts 102 102 keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1); 103 indstr1=regexp(keystr,'\\','once');%detect '\\' 104 indstr2=regexp(keystr,'\.','once');%detect '\.' 105 %indtitle=regexp(keystr,'title','once');%detect 'title'(bad characters) 106 if isempty(indstr1) && isempty(indstr2)%&&isempty(indtitle) 107 valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr); 108 if ischar(valuestr) & length(valuestr)<200 & double(valuestr)<=122 & double(valuestr)>=48 %usual characters 109 iatt_g=iatt_g+1; 110 indstr1=regexp(keystr,'\\','once');%detect '\\' 111 indstr2=regexp(keystr,'\.','once');%detect '\.' 112 if isempty(indstr1) && isempty(indstr2) 113 eval(['Data.' keystr '=''' valuestr ''';']) 114 att_key{iatt_g}=keystr; 115 end 116 elseif isempty(valuestr) 117 iatt_g=iatt_g+1; 118 eval(['Data.' keystr '=[];']) 119 att_key{iatt_g}=keystr; 103 valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr); 104 keystr=regexprep(keystr,{'\','/','\.','-',' '},{'','','','',''});%remove '\','.' or '-' if exists 105 if strcmp(keystr(1),'_') 106 keystr(1)=[]; 107 end 108 try 109 if ischar(valuestr) & length(valuestr)<200 & double(valuestr)<=122 & double(valuestr)>=48 %usual characters 110 % valuestr=regexprep(valuestr,{'\\','\/','\.','\-',' '},{'_','_','_','_','_'})%remove '\','.' or '-' if exists 111 eval(['Data.' keystr '=''' valuestr ''';']) 112 % elseif isempty(valuestr) 113 % eval(['Data.' keystr '=[];']) 120 114 elseif isnumeric(valuestr) 121 iatt_g=iatt_g+1;122 115 eval(['Data.' keystr '=valuestr;']) 123 att_key{iatt_g}=keystr; 124 end 116 else 117 eval(['Data.' keystr '='';']) 118 end 119 att_key{iatt}=keystr; 120 catch ME 121 att_key{iatt}=['attr_' num2str(iatt)]; 122 eval(['Data.' att_key{iatt} '=[];']) 125 123 end 126 124 end … … 184 182 attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1); 185 183 valuestr= netcdf.getAtt(nc,var_index(ivar)-1,attname); 184 attname=regexprep(attname,{'\','/','\.','-',' '},{'','','','',''});%remove '\','.' or '-' if exists 185 if strcmp(attname(1),'_') 186 attname(1)=[]; 187 end 188 try 186 189 if ischar(valuestr) 190 % valuestr=regexprep(valuestr,{'\\','\/','\.','\-',' '},{'_','_','_','_','_'});%remove '\','.' or '-' if exists 187 191 eval(['Data.VarAttribute{ivar}.' attname '=''' valuestr ''';']) 188 192 elseif isempty(valuestr) … … 190 194 elseif isnumeric(valuestr) 191 195 eval(['Data.VarAttribute{ivar}.' attname '=valuestr;']) 196 end 197 catch ME 198 display(attname) 199 display(valuestr) 200 display(ME.message) 201 eval(['Data.VarAttribute{ivar}.atrr_' num2str(iatt) '=''not read'';']) 192 202 end 193 203 end
Note: See TracChangeset
for help on using the changeset viewer.