function [model,desc] = fitLibrary(name,file)
%
% [model,desc] = fitLibrary(name)
%
% Returns a model function handle from the fit library. "name" is the name
% of a library function. Run fitLibrary with no inputs to display a list
% of all library functions, or a group name to display all functions within
% a group.
%
% model - a function handle to the model function
% desc - model description
%
% Groups:
% poly
% exp
% thermal
LIBRARY_NAME = 'defaultChiSquareModels.mat';
if ~exist('file','var');
file = LIBRARY_NAME;
end
load(file);
groupNames = {models.group};
for i=1:length(models)
for j=1:length(models(i).children)
childNames{j,i}=models(i).children(j).name;
end
end
% Display all models if nothing entered
if ~exist('name','var')
disp(sprintf('Note: all models are vectorized\n'));
for i=1:length(models)
dispGroup(groupNames{i},models)
disp(sprintf(' '));
end
% Display all groups if 'groups' is entered
elseif strcmp(name,'groups')
dispstr = '';
for i=1:length(models)
dispstr = [dispstr sprintf('''%s''\n',groupNames{i})];
end
disp([sprintf('Groups in library are:\n-----------------------\n')...
dispstr]);
% Display the model or group
else
ind = find(strcmp(childNames,name));
groupind = find(strcmp(groupNames,name));
% Model name is not found
if ~ind
% Name is not a group
if ~groupind
error(sprintf('Model or group ''%s'' not found in library',...
name));
% If name is a group, print the models in the group
else
dispGroup(name,models)
end
else
[i, j] = ind2sub(size(childNames),ind);
model = eval(models(j).children(i).model);
desc = models(j).children(i).desc;
end
end
end
%======= Internal functions ========================================
%------- dispGroup -------------------------------------------------
function dispGroup(groupName,models)
disp(sprintf(['Group ''%s'' contains the following models:\n'...
'-------------------------------------------------------'],...
groupName));
groups = {models.group};
groupind = find(strcmp(groups,groupName));
for i=1:length(models(groupind).children)
disp(sprintf('''%s'': %s',models(groupind).children(i).name,...
models(groupind).children(i).desc))
end
end