1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
---|
3 | <html> |
---|
4 | <head> |
---|
5 | <title>Description of check_field_structure</title> |
---|
6 | <meta name="keywords" content="check_field_structure"> |
---|
7 | <meta name="description" content="'check_field_structure': check the representation of fields by a Matlab structure"> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <meta name="generator" content="m2html © 2003 Guillaume Flandin"> |
---|
10 | <meta name="robots" content="index, follow"> |
---|
11 | <link type="text/css" rel="stylesheet" href="../m2html.css"> |
---|
12 | </head> |
---|
13 | <body> |
---|
14 | <a name="_top"></a> |
---|
15 | <div><a href="../index.html">Home</a> > <a href="index.html">.</a> > check_field_structure.m</div> |
---|
16 | |
---|
17 | <!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td> |
---|
18 | <td align="right"><a href="index.html">Index for . <img alt=">" border="0" src="../right.png"></a></td></tr></table>--> |
---|
19 | |
---|
20 | <h1>check_field_structure |
---|
21 | </h1> |
---|
22 | |
---|
23 | <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
24 | <div class="box"><strong>'check_field_structure': check the representation of fields by a Matlab structure</strong></div> |
---|
25 | |
---|
26 | <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
27 | <div class="box"><strong>function [DataOut,errormsg]=check_field_structure(Data) </strong></div> |
---|
28 | |
---|
29 | <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
30 | <div class="fragment"><pre class="comment">'check_field_structure': check the representation of fields by a Matlab structure |
---|
31 | ---------------------------------------------------------------------- |
---|
32 | function [DataOut,errormsg]=check_field_structure(Data) |
---|
33 | |
---|
34 | OUTPUT: |
---|
35 | Data: structure reproducing the input structure Data, with the additional elements: |
---|
36 | with fields: |
---|
37 | |
---|
38 | .ListDimName: cell listing the names of the array dimensions |
---|
39 | .DimValue: array dimension values (Matlab vector with the same length as .ListDimName |
---|
40 | .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName |
---|
41 | .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName |
---|
42 | errormsg: error message which is not empty when the input structure does not have the right form |
---|
43 | |
---|
44 | INPUT: |
---|
45 | Data: structure containing |
---|
46 | (optional) .ListGlobalAttribute: cell listing the names of the global attributes |
---|
47 | .Att_1,Att_2... : values of the global attributes |
---|
48 | (requested) .ListVarName: list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} ) |
---|
49 | (requested) .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells) |
---|
50 | (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName</pre></div> |
---|
51 | |
---|
52 | <!-- crossreference --> |
---|
53 | <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
54 | This function calls: |
---|
55 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
56 | </ul> |
---|
57 | This function is called by: |
---|
58 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
59 | <li><a href="plot_field.html" class="code" title="function [PlotType,PlotParamOut,haxes]= plot_field(Data,haxes,PlotParam,KeepLim,PosColorbar)">plot_field</a> 'plot_field': plot any field with the structure defined in the uvmat package</li><li><a href="struct2nc.html" class="code" title="function errormsg=struct2nc(flname,Data)">struct2nc</a> 'struct2nc': create a netcdf file from a Matlab structure</li><li><a href="struct2nc_toolbox.html" class="code" title="function errormsg=struct2nc_toolbox(flname,Data)">struct2nc_toolbox</a> 'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox</li><li><a href="uvmat.html" class="code" title="function varargout = uvmat(varargin)">uvmat</a> 'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization</li></ul> |
---|
60 | <!-- crossreference --> |
---|
61 | |
---|
62 | |
---|
63 | <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
64 | <div class="fragment"><pre>0001 <span class="comment">%'check_field_structure': check the representation of fields by a Matlab structure</span> |
---|
65 | 0002 <span class="comment">%----------------------------------------------------------------------</span> |
---|
66 | 0003 <span class="comment">% function [DataOut,errormsg]=check_field_structure(Data)</span> |
---|
67 | 0004 <span class="comment">%</span> |
---|
68 | 0005 <span class="comment">% OUTPUT:</span> |
---|
69 | 0006 <span class="comment">% Data: structure reproducing the input structure Data, with the additional elements:</span> |
---|
70 | 0007 <span class="comment">% with fields:</span> |
---|
71 | 0008 <span class="comment">%</span> |
---|
72 | 0009 <span class="comment">% .ListDimName: cell listing the names of the array dimensions</span> |
---|
73 | 0010 <span class="comment">% .DimValue: array dimension values (Matlab vector with the same length as .ListDimName</span> |
---|
74 | 0011 <span class="comment">% .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName</span> |
---|
75 | 0012 <span class="comment">% .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName</span> |
---|
76 | 0013 <span class="comment">% errormsg: error message which is not empty when the input structure does not have the right form</span> |
---|
77 | 0014 <span class="comment">%</span> |
---|
78 | 0015 <span class="comment">%INPUT:</span> |
---|
79 | 0016 <span class="comment">% Data: structure containing</span> |
---|
80 | 0017 <span class="comment">% (optional) .ListGlobalAttribute: cell listing the names of the global attributes</span> |
---|
81 | 0018 <span class="comment">% .Att_1,Att_2... : values of the global attributes</span> |
---|
82 | 0019 <span class="comment">% (requested) .ListVarName: list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} )</span> |
---|
83 | 0020 <span class="comment">% (requested) .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)</span> |
---|
84 | 0021 <span class="comment">% (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName</span> |
---|
85 | 0022 |
---|
86 | 0023 |
---|
87 | 0024 <a name="_sub0" href="#_subfunctions" class="code">function [DataOut,errormsg]=check_field_structure(Data)</a> |
---|
88 | 0025 DataOut=[]; <span class="comment">%default</span> |
---|
89 | 0026 errormsg=[]; |
---|
90 | 0027 <span class="keyword">if</span> ~isstruct(Data) |
---|
91 | 0028 errormsg=<span class="string">'input field is not a structure'</span>; |
---|
92 | 0029 <span class="keyword">return</span> |
---|
93 | 0030 <span class="keyword">end</span> |
---|
94 | 0031 <span class="keyword">if</span> isfield(Data,<span class="string">'ListVarName'</span>) && iscell(Data.ListVarName) |
---|
95 | 0032 nbfield=numel(Data.ListVarName); |
---|
96 | 0033 <span class="keyword">else</span> |
---|
97 | 0034 errormsg=<span class="string">'input field does not contain the list of variables .ListVarNames'</span>; |
---|
98 | 0035 <span class="keyword">return</span> |
---|
99 | 0036 <span class="keyword">end</span> |
---|
100 | 0037 <span class="comment">%check dimension names</span> |
---|
101 | 0038 <span class="comment">% definition by VarDimIndex (obsolete)</span> |
---|
102 | 0039 <span class="keyword">if</span> ~isfield(Data,<span class="string">'VarDimName'</span>) && isfield(Data,<span class="string">'VarDimIndex'</span>) && isfield(Data,<span class="string">'ListDimName'</span>)<span class="comment">%old convention</span> |
---|
103 | 0040 <span class="keyword">for</span> ivar=1:nbfield |
---|
104 | 0041 DimCell=Data.VarDimIndex{ivar}; |
---|
105 | 0042 <span class="keyword">if</span> isnumeric(DimCell) |
---|
106 | 0043 <span class="keyword">if</span> DimCell <= numel(Data.ListDimName) |
---|
107 | 0044 Data.VarDimName{ivar}=Data.ListDimName(DimCell); |
---|
108 | 0045 <span class="keyword">else</span> |
---|
109 | 0046 errormsg=<span class="string">'dimension names not defined'</span>; |
---|
110 | 0047 <span class="keyword">return</span> |
---|
111 | 0048 <span class="keyword">end</span> |
---|
112 | 0049 <span class="keyword">else</span> |
---|
113 | 0050 errormsg=<span class="string">'unrecognized format for .VarDimIndex'</span>; |
---|
114 | 0051 <span class="keyword">end</span> |
---|
115 | 0052 <span class="keyword">end</span> |
---|
116 | 0053 <span class="keyword">end</span> |
---|
117 | 0054 <span class="keyword">if</span> ~(isfield(Data,<span class="string">'VarDimName'</span>) && iscell(Data.VarDimName)) |
---|
118 | 0055 errormsg=<span class="string">'input field does not contain the list of dimensions .VarDimName'</span>; |
---|
119 | 0056 <span class="keyword">return</span> |
---|
120 | 0057 <span class="keyword">end</span> |
---|
121 | 0058 <span class="keyword">if</span> isfield(Data,<span class="string">'DimValue'</span>) |
---|
122 | 0059 Data=rmfield(Data,<span class="string">'DimValue'</span>); |
---|
123 | 0060 <span class="keyword">end</span> |
---|
124 | 0061 <span class="keyword">if</span> isfield(Data,<span class="string">'VarDimName'</span>) && iscell(Data.VarDimName) |
---|
125 | 0062 nbdim=0; |
---|
126 | 0063 <span class="keyword">if</span> numel(Data.VarDimName)==nbfield |
---|
127 | 0064 <span class="keyword">for</span> ivar=1:nbfield |
---|
128 | 0065 VarName=Data.ListVarName{ivar}; |
---|
129 | 0066 <span class="keyword">if</span> ~isfield(Data,VarName) |
---|
130 | 0067 errormsg=[<span class="string">'the listed variable '</span> VarName <span class="string">' is not found'</span>]; |
---|
131 | 0068 <span class="keyword">return</span> |
---|
132 | 0069 <span class="keyword">else</span> |
---|
133 | 0070 eval([<span class="string">'sizvar=size(Data.'</span> VarName <span class="string">');'</span>])<span class="comment">% sizvar = dimension of variable</span> |
---|
134 | 0071 DimCell=Data.VarDimName{ivar}; |
---|
135 | 0072 <span class="keyword">if</span> ischar(DimCell) |
---|
136 | 0073 DimCell={DimCell}; |
---|
137 | 0074 <span class="keyword">elseif</span> ~iscell(DimCell) |
---|
138 | 0075 errormsg=[<span class="string">'wrong format for .VarDimName{'</span> num2str(ivar) <span class="string">' (must be the cell of dimension names of the variable '</span> VarName]; |
---|
139 | 0076 <span class="keyword">return</span> |
---|
140 | 0077 <span class="keyword">end</span> |
---|
141 | 0078 nbcoord=numel(sizvar); |
---|
142 | 0079 <span class="comment">% Data.VarType{ivar}=[num2str(nbcoord) 'D'];%default</span> |
---|
143 | 0080 <span class="keyword">if</span> numel(DimCell)==0 |
---|
144 | 0081 errormsg=[<span class="string">'empty declared dimension .VarDimName{'</span> num2str(ivar) <span class="string">'} for '</span> VarName]; |
---|
145 | 0082 <span class="keyword">return</span> |
---|
146 | 0083 <span class="keyword">elseif</span> numel(DimCell)==1<span class="comment">% one dimension declared</span> |
---|
147 | 0084 <span class="keyword">if</span> nbcoord==2 |
---|
148 | 0085 <span class="keyword">if</span> sizvar(1)==1 |
---|
149 | 0086 <span class="comment">% Data.VarType{ivar}='row';</span> |
---|
150 | 0087 nbcoord=1; |
---|
151 | 0088 sizvar(1)=sizvar(2); |
---|
152 | 0089 <span class="keyword">elseif</span> sizvar(2)==1 |
---|
153 | 0090 <span class="comment">% Data.VarType{ivar}='column';</span> |
---|
154 | 0091 nbcoord=1; |
---|
155 | 0092 <span class="keyword">else</span> |
---|
156 | 0093 errormsg=[<span class="string">'1 dimension declared in .VarDimName{'</span> num2str(ivar) <span class="string">'} inconsistent with the nbre of dimensions =2 of the variable '</span> VarName]; |
---|
157 | 0094 <span class="keyword">return</span> |
---|
158 | 0095 <span class="keyword">end</span> |
---|
159 | 0096 <span class="keyword">else</span> |
---|
160 | 0097 errormsg=[<span class="string">'1 dimension declared in .VarDimName{'</span> num2str(ivar) <span class="string">'} inconsistent with the nbre of dimensions ='</span> num2str(nbcoord) <span class="string">' of the variable '</span> VarName]; |
---|
161 | 0098 <span class="keyword">return</span> |
---|
162 | 0099 <span class="keyword">end</span> |
---|
163 | 0100 <span class="keyword">else</span> |
---|
164 | 0101 <span class="keyword">if</span> nbcoord~=numel(DimCell) |
---|
165 | 0102 errormsg=[<span class="string">'nbre of declared dimensions in .VarDimName{'</span> num2str(ivar) <span class="string">'} inconsistent with the nbre of dimensions ='</span> num2str(nbcoord) <span class="string">' of the variable '</span> VarName]; |
---|
166 | 0103 <span class="keyword">return</span> |
---|
167 | 0104 <span class="keyword">end</span> |
---|
168 | 0105 <span class="keyword">end</span> |
---|
169 | 0106 DimIndex=[]; |
---|
170 | 0107 <span class="keyword">for</span> idim=1:nbcoord <span class="comment">%loop on the coordinates of variable #ivar</span> |
---|
171 | 0108 DimName=DimCell{idim}; |
---|
172 | 0109 testprev=0; |
---|
173 | 0110 <span class="keyword">for</span> iprev=1:nbdim <span class="comment">%check previously listed dimension names</span> |
---|
174 | 0111 <span class="keyword">if</span> strcmp(Data.ListDimName{iprev},DimName) |
---|
175 | 0112 <span class="keyword">if</span> ~isequal(Data.DimValue(iprev),sizvar(idim)) |
---|
176 | 0113 <span class="keyword">if</span> isequal(Data.DimValue(idim),0) <span class="comment">% the dimension has been already detected as range</span> |
---|
177 | 0114 Data.DimValue(idim)=sizvar(idim); <span class="comment">%update with actual value</span> |
---|
178 | 0115 <span class="keyword">elseif</span> sizvar(idim)==2 && strcmp(DimName,VarName) |
---|
179 | 0116 <span class="comment">% Data.VarType{ivar}='range'; %case of a regularly spaced coordinate defined by the first and last values</span> |
---|
180 | 0117 <span class="keyword">else</span> |
---|
181 | 0118 errormsg=[<span class="string">'dimension declaration inconsistent with the size =['</span> num2str(sizvar) <span class="string">'] for '</span> VarName]; |
---|
182 | 0119 <span class="keyword">return</span> |
---|
183 | 0120 <span class="keyword">end</span> |
---|
184 | 0121 <span class="keyword">end</span> |
---|
185 | 0122 DimIndex=[DimIndex iprev]; |
---|
186 | 0123 testprev=1; |
---|
187 | 0124 <span class="keyword">break</span> |
---|
188 | 0125 <span class="keyword">end</span> |
---|
189 | 0126 <span class="keyword">end</span> |
---|
190 | 0127 <span class="keyword">if</span> ~testprev <span class="comment">% a new dimension is appended to the list</span> |
---|
191 | 0128 nbdim=nbdim+1; |
---|
192 | 0129 <span class="keyword">if</span> sizvar(idim)==2 && strcmp(DimName,VarName) |
---|
193 | 0130 Data.DimValue(nbdim)=0; <span class="comment">%to be updated for a later variable</span> |
---|
194 | 0131 <span class="comment">% Data.VarType{ivar}='range';</span> |
---|
195 | 0132 <span class="keyword">else</span> |
---|
196 | 0133 Data.DimValue(nbdim)=sizvar(idim); |
---|
197 | 0134 <span class="keyword">end</span> |
---|
198 | 0135 Data.ListDimName{nbdim}=DimName; |
---|
199 | 0136 DimIndex=[DimIndex nbdim]; |
---|
200 | 0137 <span class="keyword">end</span> |
---|
201 | 0138 <span class="keyword">end</span> |
---|
202 | 0139 Data.VarDimIndex{ivar}=DimIndex; |
---|
203 | 0140 <span class="keyword">end</span> |
---|
204 | 0141 <span class="keyword">end</span> |
---|
205 | 0142 <span class="keyword">else</span> |
---|
206 | 0143 errormsg=<span class="string">' .ListVarNames and .VarDimName have different lengths'</span>; |
---|
207 | 0144 <span class="keyword">return</span> |
---|
208 | 0145 <span class="keyword">end</span> |
---|
209 | 0146 <span class="keyword">else</span> |
---|
210 | 0147 errormsg=<span class="string">'input field does not contain the cell of dimension names .VarDimName for variables'</span>; |
---|
211 | 0148 <span class="keyword">return</span> |
---|
212 | 0149 <span class="keyword">end</span> |
---|
213 | 0150 DataOut=Data; |
---|
214 | 0151 |
---|
215 | 0152</pre></div> |
---|
216 | <hr><address>Generated on Fri 13-Nov-2009 11:17:03 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address> |
---|
217 | </body> |
---|
218 | </html> |
---|