i my program for function 'slover' for manipulating structures. but there is error on running program -- ??? Error using ==>class The CLASS function must be called from a class constructor. Error in ==>slover at 172 o = class(defstruct, myclass)

1 view (last 30 days)
code is ------------
function [o, others] = slover(params, others, varargin) % class constructor for slice overlay (slover) object % FORMAT [o, others] = slover(params, others, varargin) % % Inputs % params - either: % - action string implementing class methods (see below) % - array of image names / vol structs to display % - structure with some fields for object (see below) % others - structure, containing extra fields for object (or children) % varargin - maybe some other parameters for action calls (see below) % % Outputs % o - slover object % others - any unrecognized fields from params, others % % Object fields are: % - img - array of structs with information for images to display % - img structs contain fields % type - one of {'truecolour' 'split', 'contour'}; % truecolour - displays transparent (see prop) image % overlaid with any previous % split - in defined area, replaces image present (SPM % type activation display) % contour - contour map of image overlaid. See help % for contours function in matlab % vol - vol struct info (see spm_vol) % can also be vol containing image as 3d matrix % set with add_blobs method % cmap - colormap for this image % nancol - color for NaN. If scalar, this is an index into % the image cmap. If 1x3 vector, it's a colour % prop - proportion of intensity for this cmap/img % func - function to apply to image before scaling to cmap % (and therefore before min/max thresholding. E.g. a func of % 'i1(i1==0)=NaN' would convert zeros to NaNs % range - 2x1 vector of values for image to distribute colormap across % the first row of the colormap applies to the first % value in 'range', and the last value to the second % value in 'range' % outofrange - behavior for image values to the left and % right of image limits in 'range'. Left means % colormap values < 1, i.e for image values < % range(1), if (range(1)<range(2)), and image values > % range(1) where (range(1)>range(2)). If missing, % display min (for Left) and max (for Right) value from colormap. % Otherwise should be a 2 element cell array, where % the first element is the colour value for image values % left of 'range', and the second is for image values % right of 'range'. Scalar values for % colour index the colormap, 3x1 vectors are colour % values. An empty array attracts default settings % appropriate to the mode - i.e. transparent colour (where % img(n).type is truecolour), or split colour. Empty cells % default to 0. 0 specifies that voxels with this % colour do not influence the image (split = % background, true = black) % hold - resampling order for image (see spm_sample_vol) - % default 1 % background - value when resampling outside image - default % NaN % linespec - string, applies only to contour map, % e.g. 'w-' for white continuous lines % contours - vector, applies to contour map only, defines % values in image for which to show contours % (see help contours) % linewidth - scalar, width in points of contour lines % % - transform - either - 4x4 transformation to apply to image slice position, % relative to mm given by slicedef, before display % or - text string, one of axial, coronal, sagittal % These orientations assume the image is currently % (after its mat file has been applied) axially % oriented % - slicedef - 2x3 array specifying dimensions for slice images in mm % where rows are x,and y of slice image, and cols are neg max dim, % slice separation and pos max dim % - slices - vector of slice positions in mm in z (of transformed image) % - figure - figure handle for slice display figure % The object used for the display is attached as 'UserData' % to this figure % - figure_struct - stored figure parameters (in case figure dies and % needs to be recreated) % - refreshf - flag - if set or empty, refresh axis info for figure % else assume this is OK % - clf - flag, non zero -> clear figure before display. Redundant % if refreshf == 0 % - resurrectf - if not zero, and figure (above) does not exist, will % attempt to recreate figure with same area properties. % Otherwise painting will give an error. % - userdata - flag, non zero -> attaches object to figure when ploting, % for use by callbacks (default is 1) % - area - struct with fields % position - bottom left, x size y size 1x4 vector of % area in which to display slices % units - one of % inches,centimeters,normalized,points,{pixels} % halign - one of left,{center},right % valign - one of top,{middle},bottom % - xslices - no of slices to display across figure (defaults to an optimum) % - cbar - if empty, missing, no colourbar. If an array of integers, then % indexes img array, and makes colourbar for each cmap for % that img. Cbars specified in order of appearance L->R % - labels - struct can be: % - empty (-> default numerical labels) % - 'none' (string) (no labels) % - or contain fields: % colour - colour for label text % size - font size in units normalized to slice axes % format - if = cell array of strings = % labels for each slice in Z. If is string, specifies % sprintf format string for labelling in distance of the % origin (Xmm=0, Ymm=0) of each slice from plane containing % the AC, in mm, in the space of the transformed image % - callback - callback string for button down on image panels. THe % following examples assume that you have the 'userdata' % field set to 1, giving you access to underlying object % To print to the matlab window the equivalent position in % mm of the position of a mouse click on one of the image % slices, set callback to: % 'get_pos(get(gcf, ''UserData''))' % To print the intensity values of the images at the clicked point: % ['so_obj = get(gcf, ''UserData''); ' ... % 'point_vals(so_obj, get_pos(so_obj))'] % - printstr - string for printing slice overlay figure window, e.g. % 'print -dpsc -painters -noui' (the default) % - printfile - name of file to print output to; default 'slices.ps' % % Action string formats: % FORMAT [cmap warnstr] = slover('getcmap', cmapname) % Gets colormap named in cmapname string % % FORMAT [mx mn] = slover('volmaxmin', vol) % Returns maximum and minimum finite values from vol struct 'vol' % % FORMAT vol = slover('blobs2vol', XYZ, vals, mat) % returns (pseudo) vol struct for 3d blob volume specified % in matrices as above % % FORMAT vol = slover('matrix2vol', mat3d, mat) % returns (pseudo) vol struct for 3d matrix % input matrices as above % % FORMAT obj = slover('basic_ui' [,dispf]) % Runs basic UI to fetch some parameters, does display, returns object % If optional dispf parameter = 0, supresses display % % $Id: slover.m,v 1.2 2005/05/06 22:59:56 matthewbrett Exp $
myclass = 'slover';
% Default object structure defstruct = struct('img', [], ... 'transform', 'axial', ... 'slicedef', [], ... 'slices', [], ... 'figure', [], ... 'figure_struct', [], ... 'refreshf', 1, ... 'clf', 1, ... 'resurrectf', 1, ... 'userdata', 1, ... 'area', [], ... 'xslices', [], ... 'cbar', [], ... 'labels', [], ... 'callback', ';', ... 'printstr', 'print -dpsc -painters -noui', ... 'printfile', 'slices.ps');
if nargin < 1 o = class(defstruct, myclass); others = []; return end if nargin < 2 others = []; end
% parse out string action calls (class functions) if ischar(params) switch params case 'getcmap' if nargin < 2 error('Need colormap name'); end o = pr_getcmap(others); return case 'volmaxmin' if nargin < 2 error('Need volume to calculate max/min'); end [o others] = pr_volmaxmin(others); return case 'blobs2vol' if nargin < 4 error('Need XYZ, vals, mat'); end o = pr_blobs2vol(others, varargin{:}); return case 'matrix2vol' if nargin < 3 error('Need matrix and mat'); end o = pr_matrix2vol(others, varargin{:}); return case 'basic_ui' o = pr_basic_ui(others, varargin{:}); if ~isempty(o), o = paint(o); end return end
% if not action string, must be filename(s)
params = spm_vol(params);
end
% Could these just be image vol structs? if isfield(params, 'fname') for i = 1:prod(size(params)) obj.img(i).vol = params(i); end params = obj; end
% Deal with passed objects of this (or child) class if isa(params, myclass) o = params; % Check for simple form of call if isempty(others), return, end
% Otherwise, we are being asked to set fields of object
[p others] = mars_struct('split', others, defstruct);
o = mars_struct('ffillmerge', o, p);
return
end
% fill params with defaults, parse into fields for this object, children params = mars_struct('fillafromb', params, others); [params, others] = mars_struct('ffillsplit', defstruct, params);
% set the slover object o = class(params, myclass);
% refill with defaults o = fill_defaults(o);
return
function varargout = mars_struct(action, varargin) % multifunction function for manipulating structures % % To help the exposition a bit: % 'fill' in a name, means that values empty or missing % in one structure are fetched from another % % 'merge' means simply that missing fields are added, with % values, from a second structure (but not filled if empty) % % Each function needs to deal with the case of empty arguments % % FORMAT c = mars_struct('fillafromb', a, b, fieldns, flags) % fills structure fields empty or missing in a from those present in b % a, b are structures % fieldns (optional) is cell array of field names to fill from in b % c is returned structure % Is recursive, will fill struct fields from struct fields % flags may contain 'f', which Force fills a from b (all non empty % fields in b overwrite those in a) % flags may also contain 'r', which Restricts fields to write from b, to % those that are already present in a % % FORMAT [c, d] = mars_struct('split', a, b) % split structure a into two, according to fields in b % so that c becomes a structure which contains the fields % in a, that are also present in b, and d contains the fields % in a that are not present in b. b can be a structure % or a cell array of fieldnames % % FORMAT [d] = mars_struct('strip', a, b) % strips all fields present in b from those in a, % returning denuded structure as d. b can be a structure % or a cell array of fieldnames. 'strip' is just 'split' % but returning only the second argument % % FORMAT c = mars_struct('merge', a, b) % merges structure a and b (fields present in b added to a) % % FORMAT [c,d] = mars_struct('ffillsplit', a, b) % force fill, followed by split % All fields from a, that are also present in b, and not empty in b, % are replaced with the values in b; the result is returned as c % Any fields present in a, but not present in b, are returned in d % % FORMAT c = mars_struct('ffillmerge', a, b) % force fill followed by merge % performs 'ffillsplit' on a and b, then merges a and b % All fields present in a or b are returned in c, but % any fields present in both, now have the value from b % % FORMAT [c d] = mars_struct('splitmerge', a, b) % performs 'split' on a and b, creating c and d % then merges c with b. % d contains fields in a that were not present in b % c contains fields present in both, or just in b % % FORMAT z = mars_struct('isthere', a, b [, c [, d ...]) % returns 1 if field named in b is present in a % and field value is not empty. % The call is recursive if more than two arguments are passed % Thus with structure s = struct('one', struct('two', 3)) % mars_struct('isthere', s, 'one', 'two') returns 1 % % FORMAT z = mars_struct('getifthere', a, b [, c [, d ...]) % returns value of field named in b from a or [] if absent % Call is recursive, like 'isthere' above. % % FORMAT strs = mars_struct('celldisp', a) % returns output like disp(a) as a cell array % Useful for printing text description of structure % % $Id: mars_struct.m,v 1.13 2004/09/22 16:02:38 matthewbrett Exp $
if nargin < 1 error('Action needed'); end if nargin < 2 error('Must specify structure') end if nargin < 3 varargin = {varargin{:} []}; end [a b] = deal(varargin{1:2});
switch lower(action) case 'fillafromb' % Return for empty passed args if isempty(a), varargout = {b}; return, end if isempty(b), varargout = {a}; return, end if nargin < 4, fieldns = []; else fieldns = varargin{3}; end if isempty(fieldns) if ~isstruct(b), error('Need struct as 2nd argument'); end fieldns = fieldnames(b); end if nargin < 5, flags = ''; else flags = varargin{4}; end if isempty(flags), flags = ' ';end
if ischar(fieldns), fieldns=cellstr(fieldns);end
af = fieldnames(a)';
bf = fieldns';
% classify fields 0 = a~b, 1 = a&b, 2=b~a
cf = af;
ftype = ismember(af, bf);
if ~any(flags == 'r')
b_not_a = find(~ismember(bf, af));
cf = {cf{:} bf{b_not_a}};
ftype = [ftype ones(1, length(b_not_a))*2];
end
% cope with arrays of structures
alen = prod(size(a));
blen = prod(size(b));
maxlen = max(alen, blen);
for si=1:maxlen
ctmp = [];
for i=1:length(cf)
fn = cf{i};
switch ftype(i)
case 0 % a~b
fval = getfield(a(si), fn);
case 1 % shared field
bfc = getfield(b(si), fn);
if isempty(getfield(a(si), fn)) | ... % a field is empty
(any(flags == 'f' & ~isempty(bfc)))% or force fill
fval = bfc;
else % field not empty, could be struct -> recurse
fval = getfield(a(si),fn);
if isstruct(fval) & isstruct(bfc)
fval = mars_struct('fillafromb',fval,bfc);
end
end
case 2 % b~a
fval = getfield(b(si), fn);
case 3 % no field information, see below
fval = [];
end
if isempty(ctmp)
ctmp = struct(fn, fval);
else
ctmp = setfield(ctmp, fn, fval);
end
end
c(si) = ctmp;
if si == blen % reached end of bs, rest of b~a fields are empty
ftype = (ftype == 2) * 3;
elseif si == alen % end of a's rest of a~b fields are empty
ftype = (ftype == 0) * 2 + 1;
end
end
varargout = {c};
case 'split'
if isempty(a), varargout = {a,a}; return, end
if isempty(b), varargout = {b,a}; return, end
d = a;
c = [];
if ischar(b), b = {b};end
if isstruct(b), b = fieldnames(b);end
for bf = b(:)'
if isfield(a, bf{1})
c = setfield(c, bf{1}, getfield(a, bf{1}));
d = rmfield(d, bf{1});
end
end
varargout = {c, d};
case 'strip'
[c d] = mars_struct('split', a, b);
varargout = {d};
case 'merge'
if isempty(a), varargout = {b}; return, end
if isempty(b), varargout = {a}; return, end
c = a;
for bf = fieldnames(b)';
if ~isfield(a, bf{1})
c = setfield(c, bf{1}, getfield(b, bf{1}));
end
end
varargout = {c};
case 'ffillsplit'
if isempty(a) | isempty(b)
% Nothing in common, return unchanged
varargout = {a, b}; return
end
c = a; d = b;
cf = fieldnames(c);
for i=1:length(cf)
if isfield(d, cf{i})
dfc = getfield(d,cf{i});
if ~isempty(dfc)
c = setfield(c, cf{i}, dfc);
end
d = rmfield(d, cf{i});
end
end
varargout = {c,d};
case 'ffillmerge'
[a b] = mars_struct('ffillsplit', a, b);
varargout = {mars_struct('merge', a, b)};
case 'splitmerge'
[a c] = mars_struct('split', a, b);
varargout = {mars_struct('merge', a, b) c};
case 'isthere'
if isempty(a), varargout = {0}; return, end
c = mars_struct('getifthere', varargin{:});
varargout = {~isempty(c)};
case 'getifthere'
if isempty(a), varargout = {[]}; return, end
if isempty(b), varargout = {[]}; return, end
for v = 2:nargin-1
b = varargin{v};
if ~isfield(a, b)
varargout = {[]};
return
end
a = getfield(a, b);
end
varargout = {a};
case 'celldisp'
if isempty(a), varargout = {{}}; return, end
af = fieldnames(a);
c = {};
pad_len = size(char(af), 2) + 4;
pad_str = ['%' num2str(pad_len) 's: %s'];
for f = 1:length(af)
d = getfield(a, af{f});
cls = class(d);
sz = size(d);
szstr = sprintf('%dx', size(d));
szstr(end) = [];
switch cls
case 'char'
case {'double', 'float'}
d = ['[' num2str(d) ']'];
otherwise
d = sprintf('[%s %s]', szstr, cls);
end
c{f} = sprintf(pad_str, af{f}, d);
end
varargout = {c};
otherwise
error(['Suspicious action was ' action]);
end % switch

Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!