function found_models = search_save_models(dir)
% looks for models (modelZA, modelMAe, modelMAn, modelMAp, modelETA,
% modelKSI). Tells in dialog box which models were found in given directory , and gives a
% matrix of size (1x6) with 0 or 1 whether the models exist or no.
% The models are saved as a variable models_indir.mat!
newpath=dir;%path where to search for models
%addpath(newpath);
thisdir=cd; %save the working directory
cd(newpath);
%look if any models are available
test=[exist('modelZA.mat'),exist('modelMAe.mat'),exist('modelMAn.mat'),exist('modelMAp.mat'),exist('modelETA.mat'),exist('modelKSI.mat')];
if sum(test)==0;
errordlg('No models where found in the searchpaths of Matlab','Bad Input','modal')
cd(thisdir)
else
if test(1)~=0
a=which('modelZA.mat');%which is the nearest modelZA.mat file
b=fullfile(newpath,'modelZA.mat');%where did matlab find a file
if strcmp(a,b) % if the found file is the one we are looking for
load('modelZA.mat'); %load all workspace variables of modelZA, one is the modelZA object
found_models(1)=1;
models.ZA = modelZA;
end
else
found_models(1)=0;
end
%cd(thisdir);
%cd(newpath);
if test(2)~=0
a=which('modelMAe.mat');
b=fullfile(newpath,'modelMAe.mat');
if strcmp(a,b)
load('modelMAe.mat'); %load all workspace variables of modelZA, one is the modelZA object
found_models(2)=1;
models.MAe=modelMAe;
end
else
found_models(2)=0;
end
end
%cd(thisdir);
%cd(newpath);
if test(3)~=0
a=which('modelMAn.mat');
b=fullfile(newpath,'modelMAn.mat');
if strcmp(a,b)
load('modelMAn.mat'); %load all workspace variables of modelZA, one is the modelZA object
found_models(3)=1;
models.MAn= modelMAn;
end
else
found_models(3)=0;
end
%cd(thisdir);
%cd(newpath);
if test(4)~=0
a=which('modelMAp.mat');
b=fullfile(newpath,'modelMAp.mat');
if strcmp(a,b)
load('modelMAp.mat'); %load all workspace variables of modelZA, one is the modelZA object
found_models(4)=1;
models.MAp= modelMAp;
end
else
found_models(4)=0;
end
if test(5)~=0
a=which('modelETA.mat');
b=fullfile(newpath,'modelETA.mat');
if strcmp(a,b)
load('modelETA.mat'); %load all workspace variables of modelZA, one is the modelZA object
found_models(5)=1;
models.ETA= modelETA;
end
else
found_models(5)=0;
end
if test(6)~=0
a=which('modelKSI.mat');
b=fullfile(newpath,'modelKSI.mat');
if strcmp(a,b)
load('modelKSI.mat'); %load all workspace variables of modelZA, one is the modelZA object
found_models(6)=1;
model.KSI= modelKSI;
end
else
found_models(6)=0;
end
cd(thisdir);
model_names=[
'ModelZA '
'ModelMAe '
'ModelMAn '
'ModelMAp '
'ModelETA '
'ModelKSI '
];
model_names=cellstr(model_names);
n=1;
for k=1:length(found_models)
if found_models(k)==1
present_model_names(n)=model_names(k);
n=n+1;
end
end
if sum(found_models)==0
errordlg('No models where found in this directory','Bad Input','modal')
else
msgbox(['There are ', num2str(sum(found_models)), ' models found in this directory'
present_model_names'],'models in given directory')
save models_indir.mat -struct models;
end
found_models;
end