% Pattern Object of SPM-Beta-Images
%
%    images will be defined by using SPM.mat
%    pattern will be generated from specification of Beta-Images find in the
%    SPM.mat
classdef janis_patternBeta < janis_patternAbstractImg

   properties (SetAccess = public)
      ImgType = 'beta';
   end

    methods

        % Constructor
        %    takes
        %    * first location of the SPM.mat
        %    * second a list of Conditions as input, Strings which must match the
        %    specifications in the SPM.mat
        function obj = janis_patternBeta(SPMfile,listOfConditions)
            obj = obj@janis_patternAbstractImg;
            if nargin > 0
                obj.SPMfile = SPMfile;
                obj = addCondition(obj,SPMfile,listOfConditions);
            end
        end

        function obj = addCondition(obj,SPMfile,listOfConditions)
            obj.SPMfile = SPMfile;
            try
                load(SPMfile);
            catch excep
             fprintf('Unable to access spm.mat: %s\n', SPMfile);
             rethrow(excep);
            end
            try
                betaNames = {SPM.Vbeta.descrip};
                for condCount =1:numel(listOfConditions)
                    % find Index of Condition
                    condIdx = strcmpi(betaNames,listOfConditions{condCount});
                    if exist ( SPM.Vbeta(condIdx).fname)
                        obj = obj.addImage(SPM.Vbeta(condIdx).fname);
                    elseif exist([SPM.swd filesep SPM.Vbeta(condIdx).fname])
                        obj = obj.addImage([SPM.swd filesep SPM.Vbeta(condIdx).fname]);
                    else
                        throw(MException('janis:betaFileNotFound'));
                    end
                end
            catch excep
                % exception handling if no betas found
                    error('Unable to find all beta-images for subject: %s',SPMfile);
                    rethrow(excep);
            end
        end
    end
end