Home > rest_20090422 > rest_spm5_files > @create_file_array > subsasgn.m

subsasgn

PURPOSE ^

Overloaded subsasgn function for file_array objects.

SYNOPSIS ^

function obj = subsasgn(obj,subs,dat)

DESCRIPTION ^

 Overloaded subsasgn function for file_array objects.
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

subsasgn.m

SOURCE CODE ^

0001 function obj = subsasgn(obj,subs,dat)
0002 % Overloaded subsasgn function for file_array objects.
0003 % _______________________________________________________________________
0004 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0005 
0006 %
0007 % $Id: subsasgn.m 316 2005-11-28 16:56:43Z john $
0008 
0009 
0010 if isempty(subs)
0011     return;
0012 end;
0013 if ~strcmp(subs(1).type,'()'),
0014     if strcmp(subs(1).type,'.'),
0015         %error('Attempt to reference field of non-structure array.');
0016         if numel(struct(obj))~=1,
0017             error('Can only change the fields of simple create_file_array objects.');
0018         end;
0019         switch(subs(1).subs)
0020         case 'fname',      obj = asgn(obj,@fname,    subs(2:end),dat); %fname(obj,dat);
0021         case 'dtype',      obj = asgn(obj,@dtype,    subs(2:end),dat); %dtype(obj,dat);
0022         case 'offset',     obj = asgn(obj,@offset,   subs(2:end),dat); %offset(obj,dat);
0023         case 'dim',        obj = asgn(obj,@dim,      subs(2:end),dat); %obj = dim(obj,dat);
0024         case 'scl_slope',  obj = asgn(obj,@scl_slope,subs(2:end),dat); %scl_slope(obj,dat);
0025         case 'scl_inter',  obj = asgn(obj,@scl_inter,subs(2:end),dat); %scl_inter(obj,dat);
0026         otherwise, error(['Reference to non-existent field "' subs.type '".']);
0027         end;
0028         return;
0029     end;
0030     if strcmp(subs(1).type,'{}'), error('Cell contents reference from a non-cell array object.'); end;
0031 end;
0032 
0033 if numel(subs)~=1, error('Expression too complicated');end;
0034 dm   = size(obj);
0035 sobj = struct(obj);
0036 
0037 if length(subs.subs) < length(dm),
0038     l   = length(subs.subs);
0039     dm  = [dm(1:(l-1)) prod(dm(l:end))];
0040     if numel(sobj) ~= 1,
0041         error('Can only reshape simple create_file_array objects.');
0042     end;
0043     if numel(sobj.scl_slope)>1 || numel(sobj.scl_inter)>1,
0044         error('Can not reshape create_file_array objects with multiple slopes and intercepts.');
0045     end;
0046 end;
0047 
0048 dm   = [size(obj) ones(1,16)];
0049 do   = ones(1,16);
0050 args = {};
0051 for i=1:length(subs.subs),
0052     if ischar(subs.subs{i}),
0053         if ~strcmp(subs.subs{i},':'), error('This shouldn''t happen....'); end;
0054         args{i} = int32(1:dm(i));
0055     else
0056         args{i} = int32(subs.subs{i});
0057     end;
0058     do(i) = length(args{i});
0059 end;
0060 if length(sobj)==1
0061     sobj.dim = dm;
0062     if numel(dat)~=1,
0063         subfun(sobj,double(dat),args{:});
0064     else
0065         dat1 = double(dat) + zeros(do);
0066         subfun(sobj,dat1,args{:});
0067     end;
0068 else
0069     for j=1:length(sobj),
0070         ps  = [sobj(j).pos ones(1,length(args))];
0071         dm  = [sobj(j).dim ones(1,length(args))];
0072         siz = ones(1,16);
0073         for i=1:length(args),
0074             msk      = args{i}>=ps(i) & args{i}<(ps(i)+dm(i));
0075             args2{i} = find(msk);
0076             args3{i} = int32(double(args{i}(msk))-ps(i)+1);
0077             siz(i)   = numel(args2{i});
0078         end;
0079         if numel(dat)~=1,
0080             dat1 = double(subsref(dat,struct('type','()','subs',{args2})));
0081         else
0082             dat1 = double(dat) + zeros(siz);
0083         end;
0084         subfun(sobj(j),dat1,args3{:});
0085     end
0086 end
0087 return
0088 
0089 function sobj = subfun(sobj,dat,varargin)
0090 va = varargin;
0091 
0092 dt  = datatypes;
0093 ind = find(cat(1,dt.code)==sobj.dtype);
0094 if isempty(ind), error('Unknown datatype'); end;
0095 if dt(ind).isint, dat(~finite(dat)) = 0; end;
0096 
0097 if ~isempty(sobj.scl_inter),
0098     inter = sobj.scl_inter;
0099     if numel(inter)>1,
0100         inter = resize_scales(inter,sobj.dim,varargin);
0101     end;
0102     dat = double(dat) - inter;
0103 end;
0104 
0105 if ~isempty(sobj.scl_slope),
0106     slope = sobj.scl_slope;
0107     if numel(slope)>1,
0108         slope = resize_scales(slope,sobj.dim,varargin);
0109         dat   = double(dat)./slope;
0110     else
0111         dat   = double(dat)/slope;
0112     end;
0113 end;
0114 
0115 if dt(ind).isint, dat = round(dat); end;
0116 dat   = feval(dt(ind).conv,dat);
0117 nelem = dt(ind).nelem;
0118 if nelem==1,
0119     nic_mat2file(sobj,dat,va{:});
0120 elseif nelem==2,
0121     sobj1       = sobj;
0122     sobj1.dim   = [2 sobj.dim];
0123     sobj1.dtype = dt(find(strcmp(dt(ind).prec,{dt.prec}) & (cat(2,dt.nelem)==1))).code;
0124     dat         = reshape(dat,[1 size(dat)]);
0125     dat         = [real(dat) ; imag(dat)];
0126     nic_mat2file(sobj1,dat,int32([1 2]),va{:});
0127 else
0128     error('Inappropriate number of elements per voxel.');
0129 end;
0130 return
0131 
0132 function obj = asgn(obj,fun,subs,dat)
0133 if ~isempty(subs),
0134     tmp = feval(fun,obj);
0135     tmp = subsasgn(tmp,subs,dat);
0136     obj = feval(fun,obj,tmp);
0137 else
0138     obj = feval(fun,obj,dat);
0139 end;
0140

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005