function ud = load_exec(ud,file)
% LOAD_EXEC Calls 'LOAD' to load the MAT-file and sets the userdata.
%           
% Inputs:
%    ud   - userdata stored in the GUI's figure.
%    file - name of the MAT-file with specs structure to load.

%   Author(s): R. Losada.
%   Copyright 1988-2000 The MathWorks, Inc. 
%   $Revision: 1.16 $  $Date: 2000/06/16 15:57:42 $ 

% Turning warning off in case the load warns
w = warning('off'); % cache warning state

[s,successflag] = loadandcheckforcorruptfile(file,ud);
if ~successflag,
	return
end

warning(w); % Reset the warning state

% A MAT file has been loaded, but it might not contain a valid session.
if ~isavalidsession(file,s,ud),
	return
end

% Check if current_filt has been converted to a structure.
% If so, Filter Design Toolbox is not installed, can't load session.
%successflag = checkforqfilt(w,s,ud);
if ~isauncorruptedqfilt(s,ud),
	return
end


% Save the user's specs
ud.specs.design             = s.fdspecs;
ud.toolbar.analysis         = s.analysis_mode;
ud.specs.import             = s.import_specs;
ud.specs.export             = s.export_specs;
ud.specs.convert            = s.convert_specs;
ud.reffilter                = s.reference_filt;
ud.filter                   = s.current_filt;
ud.mode                     = s.mode;
ud.flags.filter_designed    = s.filter_designed;  
ud.flags.filter_imported    = s.filter_imported;  

if ud.flags.fdqexists,
	% Set quantized specific fields
	ud = setquantizedfields(ud,s);
end


% Set appropriate flags
ud.flags.file_dirty = 0;
ud.flags.overwrite  = 1;      % Allow file overwrites from now on

ud.filename.session = file;   % Save current file name

%----------------------------------------------------------------
function [s,successflag] = loadandcheckforcorruptfile(file,ud)

successflag = 1; % Assume success
s = struct([]);
try
    % Load session file.
    s = load(file,'-mat');  
catch
    % Error if the session file could not load.
    errStr = [file,'  is not a valid FDATool session file.'];
    lasterr(errStr);
    fdatoolerror(ud.h.Fig);
	successflag = 0;  
	return
end

%-----------------------------------------------------------------
function successflag = isavalidsession(file,s,ud)
% A MAT file has been loaded, but it might not contain a valid session.
% There may be other problems later, but these variables being
% present is a good indication that the file is okay.

successflag = 1; % Assume success by default

if ~isfield(s,'current_filt') | ~isfield(s,'fdspecs') | ~isfield(s,'analysis_mode')
    errStr = [file,'  is not a valid FDATool session file.'];
    lasterr(errStr);
    fdatoolerror(ud.h.Fig);
    successflag = 0;
	return
end

%--------------------------------------------------------------------
function successflag = isauncorruptedqfilt(s,ud)

successflag = 1; % Assume success by default

errStr = ['Can''t load session.  This session contains a quantized filter which requires ',... 
            'the Filter Design Toolbox.  Loading a default session.'];

if isstruct(s.current_filt),
	% If current_filt is not an object but a structure, fd tlbx is not present
    lasterr(errStr);
    fdatoolerror(ud.h.Fig);
    successflag = 0;
	return
end

%--------------------------------------------------------------------
function ud = setquantizedfields(ud,s)

% Get the last error
lasterrorstr = lasterr;
try,
	ud.flags.quantizationswitch = s.quantizationswitch;
	ud.flags.filter_quantized   = s.filter_quantized;  
	ud.specs.quantize           = s.quantize_specs; 
catch
	% If these were not saved, use default values
	ud.flags.filter_quantized = 1;
	ud.flags.quantizationswitch = 0;
	ud.specs.quantize.refqfilt = qfilt;
end
% Reset the last error
lasterr(lasterrorstr);

% [EOF] - load_exec.m
