0001 function [rs,r_nos] = region(o, r_nos, new_data, fieldname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 r = n_regions(o);
0026 if nargin < 2
0027 r_nos = [];
0028 end
0029 if isempty(r_nos)
0030 r_nos = 1:r;
0031 end
0032 if any(r_nos > r)
0033 error('Region numbers too large');
0034 end
0035
0036 st = y_struct(o);
0037 r_f = isfield(st, 'regions');
0038 y_f = isfield(st, 'Y');
0039 def_r_st = struct('name', '',...
0040 'descrip', '',...
0041 'Y', [],...
0042 'weights', [],...
0043 'info', [],...
0044 'vXYZ', [],...
0045 'mat', []);
0046 sum_func = sumfunc(o);
0047 r_len = length(r_nos);
0048
0049 if nargin < 3
0050
0051 if ~r_len, rs = {}; return, end
0052 for i = 1:r_len
0053 r_st = [];
0054 if r_f
0055 r_st = st.regions{r_nos(i)};
0056 end
0057 r_st = mars_struct('fillafromb', def_r_st, r_st);
0058 if isempty(r_st.Y)
0059 if y_f
0060 r_st.Y = st.Y(:,r_nos(i));
0061 end
0062 end
0063 rs{i} = r_st;
0064 end
0065 return
0066 end
0067
0068
0069 if nargin > 3
0070
0071 if ~ismember(fieldname, fieldnames(def_r_st))
0072 error(['Funny data field passed: ' fieldname]);
0073 end
0074 end
0075
0076 if ~iscell(new_data), new_data = {new_data}; end
0077 if length(new_data) ~= r_len
0078 error('Different numbers of new data cells and regions');
0079 end
0080 if ~r_len, rs = o; return, end
0081
0082 N = n_time_points(o);
0083
0084
0085 st.regions = region(o);
0086
0087
0088 re_sum_f = 0;
0089
0090 for i = 1:r_len
0091 r = r_nos(i);
0092 r_st = st.regions{r};
0093 if nargin > 3
0094 n_st = setfield([], fieldname, new_data{i});
0095 else
0096 n_st = new_data{i};
0097 end
0098
0099
0100
0101 if isfield(n_st, 'Y')
0102 re_sum_f = 1;
0103 if size(n_st.Y, 1) ~= N
0104 error('Incorrect number of time points in data set call');
0105 end
0106 end
0107 if isfield(n_st, 'weights')
0108 if strcmp(sum_func, 'wtmean')
0109 re_sum_f = 1;
0110 end
0111 if ~isempty(n_st.weights) & size(n_st.weights, 1) ~= N
0112 error('Incorrect number of time points in weight set call');
0113 end
0114 end
0115 st.regions{r} = mars_struct('ffillmerge', st.regions{r}, n_st);
0116 end
0117
0118 if re_sum_f
0119 st = mars_struct('strip', st, {'Y','Yvar'});
0120 rs = resummarize(y_struct(o, st));
0121 else
0122 rs = y_struct(o, st);
0123 end