Code covered by the BSD License  

Highlights from
GUI for Multivariate Image Analysis of 4-dimensional data

image thumbnail
from GUI for Multivariate Image Analysis of 4-dimensional data by Kateryna Artyushkova
Multivariate Image Analysis of 4-dimensional image sequences using 2-step two-way and three-way ...

four_way_mva(varargin)
function varargout = four_way_mva(varargin)
% 
% This GUI includes is a set of multivariate image analysis methods for analyzing image data sets acquired at two variables. 
% For example: emission excitation image data, spectral or dynamic (temporal) sequences of images acquired at different depths using microscopy.
%
% Two approaches are included: 
% 1. 2-step two-way MIA using PCA, MCR and Simplisma. In this method, image sequences as a function of variable1 
% at fixed variable2 are analyzed by two-way method during the 1st step and then the resulted score images at each 
% variable 2 are combined into a new data set and are analyzed by the same two-way method at the 2nd step.
%
% 2. Three-way analysis using Parafac, Tucker.  and three-way augmented MCR and MAF methods. 
% Nonnegativity constraints are imposed during all three model fitting. All three methods will result in score image
% and associated loadings as a function of both variables.
%
% Please refer to a User Guide for more details. 
% Test image data set is included.
%
% Memory extensive! Might need to spend some time to get your images into the GUI ? resize, subset or bin for it to work.
%
% Requirements: Image Processing toolbox, PLS_toolbox.
%
% GUI USES LARGE NUMBER OF SUBSET FUNCTIONS. I WAS TRYING NOT TO FORGET TO INCLUDE ALL \
% OF THEM. BUT IF YOU GET AN ERROR MISSING ANY OF THEM, PLEASE E-MAIL ME DIRECTLY.
%
% Written by K. Artyushkova
% 12/17/2010
%
% Kateryna Artyushkova
% Research Associate professor
% Department of Chemical and Nuclear Engineering
% The University of New Mexico
% (505) 277-0750
% Last Modified by GUIDE v2.5 14-Dec-2010 13:57:53
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @four_way_mva_OpeningFcn, ...
                   'gui_OutputFcn',  @four_way_mva_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before four_way_mva is made visible.
function four_way_mva_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to four_way_mva (see VARARGIN)

% Choose default command line output for four_way_mva
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes four_way_mva wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = four_way_mva_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slider_var1_Callback(hObject, eventdata, handles)
% hObject    handle to slider_var1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

data=handles.data;
[n,m,p,q]=size(data);
C1=handles.C1;
C2=handles.C2;

step=1/p;
slider_step(1)=step;
slider_step(2)=step;
set(handles.slider_var1, 'SliderStep', slider_step, 'Max', p, 'Min',0)
i=get(hObject,'Value');
i=round(i);
if i==0
    i=1;
elseif i>=p
        i=p;
else i=i;
end
C1=i;
set(handles.Cur_1,'string',C1);

axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,C1,C2)), colormap(gray)
handles.C1=C1;
handles.C2=C2;

guidata(hObject,handles)



% --- Executes during object creation, after setting all properties.
function slider_var1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_var1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_var2_Callback(hObject, eventdata, handles)
% hObject    handle to slider_var2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

data=handles.data;
[n,m,p,q]=size(data);
C1=handles.C1;
C2=handles.C2;
step=1/q;
slider_step(1)=step;
slider_step(2)=step;
set(handles.slider_var2, 'SliderStep', slider_step, 'Max', q, 'Min',0)
i=get(hObject,'Value');
i=round(i);
if i==0
    i=1;
elseif i>=q
        i=q;
else i=i;
end
C2=i;
set(handles.Cur_2,'string',C2);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,C1,C2)), colormap(gray)
handles.C1=C1;
handles.C2=C2;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function slider_var2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_var2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function Cur_1_Callback(hObject, eventdata, handles)
% hObject    handle to Cur_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Cur_1 as text
%        str2double(get(hObject,'String')) returns contents of Cur_1 as a double
data=handles.data;
[n,m,p,q]=size(data);
C1=handles.C1;
C2=handles.C2;
C1=str2double(get(hObject,'String')) ;
set(handles.Cur_1,'string',C1);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,C1,C2)), colormap(gray)
handles.C1=C1;
handles.C2=C2;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function Cur_1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Cur_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function Cur_2_Callback(hObject, eventdata, handles)
% hObject    handle to Cur_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Cur_2 as text
%        str2double(get(hObject,'String')) returns contents of Cur_2 as a double

data=handles.data;
[n,m,p,q]=size(data);
C1=handles.C1;
C2=handles.C2;
C2=str2double(get(hObject,'String'));
set(handles.Cur_2,'string',C2);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,C1,C2)), colormap(gray)
handles.C1=C1;
handles.C2=C2;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function Cur_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Cur_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --------------------------------------------------------------------
function Load_mat_Callback(hObject, eventdata, handles)
% hObject    handle to Load_mat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=lddlgpls;
data=double(data);
handles.data=data;
handles.or_data=data;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,1,1)), colormap(gray)
[n,m,p,q]=size(data);
set(handles.Min_1,'string',1);
set(handles.Max_1,'string',p);
set(handles.Cur_1,'string',1);
set(handles.Min_2,'string',1);
set(handles.Max_2,'string',q);
set(handles.Cur_2,'string',1);
prompt={'Variable 1:','Variable2:'};
def={'Wavelength','Depth'};
dlgTitle='Enter the title of variables';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
set(handles.title_var1,'string',answer{1});
set(handles.title_var2,'string',answer{2});
handles.tit_var1=answer{1};
handles.tit_var2=answer{2};
handles.C1=1;
handles.C2=1;
guidata(hObject,handles)

% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject    handle to File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in pca.
function pca_Callback(hObject, eventdata, handles)
% hObject    handle to pca (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
N1=handles.N1;
N2=handles.N2;
scal=handles.scal;
data=handles.data;
h = waitbar(0,'Please wait while PCA is performed...');
[n,m,p,q]=size(data);
for i=1:q
images=data(:,:,:,i);
model=imgpca(images,scal,N1);
scores=double(model.scores);
loads=model.loads;
for j=1:N1
    sc1(:,:,i,j)=scores(:,:,j);
    l1(:,i,j)=loads(:,j);
end
end
for i=1:N1
    model2=imgpca(sc1(:,:,:,i),scal,N2);
    sc2(:,:,:,i)=double(model2.scores);
    l2(:,:,i)=model2.loads;
end
close(h)
handles.pca_sc1=sc1;
handles.pca_sc2=sc2;
handles.pca_l1=l1;
handles.pca_l2=l2;
handles.sc1=sc1;
handles.sc2=sc2;
handles.l1=l1;
handles.l2=l2;
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;


figure
for i=1:N2;
subplot(2,N2,i)
imagesc(sc2(:,:,i,1));colormap(gray)
subplot(2,N2,i+N2)
plot(l2(:,i,1)), hline(0,'k--')
axis tight
xlabel(t_var2)
subplot(2,N2,1)
title('PCA results of 1st PC score images from mode 1','Position',[168 -5 9])
end

set(handles.Min_2p,'string',1);
set(handles.Max_2p,'string',q);
set(handles.Cur_2p,'string',1);
handles.model_pca=model;
handles.type='PCA';
guidata(hObject,handles)

    
function N_1_Callback(hObject, eventdata, handles)
% hObject    handle to N_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of N_1 as text
%        str2double(get(hObject,'String')) returns contents of N_1 as a double
N1=str2double(get(hObject,'String')) ;
handles.N1=N1;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function N_1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to N_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function N_2_Callback(hObject, eventdata, handles)
% hObject    handle to N_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of N_2 as text
%        str2double(get(hObject,'String')) returns contents of N_2 as a double
N2=str2double(get(hObject,'String')) ;
handles.N2=N2;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function N_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to N_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


function scal_Callback(hObject, eventdata, handles)
% hObject    handle to scal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of scal as text
%        str2double(get(hObject,'String')) returns contents of scal as a double
scal=get(hObject,'String');
handles.scal=scal;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function scal_CreateFcn(hObject, eventdata, handles)
% hObject    handle to scal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in simplisma.
function simplisma_Callback(hObject, eventdata, handles)
% hObject    handle to simplisma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
N1=handles.N1;
N2=handles.N2;
data=handles.data;
[n,m,p,q]=size(data);
for i=1:q
images=data(:,:,:,i);
data1=reshape(images,[n*m p]);
[purspec,purint,purity_spec]=simplisma_4w(data1,[1:p], 5, N1);
pur_images=reshape(purint,[n m N1]);
pur_int=purspec';
for j=1:N1
    sc1(:,:,i,j)=pur_images(:,:,j);
    l1(:,i,j)=pur_int(:,j);
end
end
for i=1:N1
    data2=sc1(:,:,:,i);
    [n2,m2,p2]=size(data2);
    data3=reshape(data2,[n2*m2 p2]);
    [purspec2,purint2,purity_spec]=simplisma_4w(data3,[1:p2], 5, N2);

    pur_images2=reshape(purint2,[n2 m2 N2]);
    pur_int2=purspec2';
    sc2(:,:,:,i)=pur_images2;
    l2(:,:,i)=pur_int2;
end

handles.simp_sc1=sc1;
handles.simp_sc2=sc2;
handles.simp_l1=l1;
handles.simp_l2=l2;
handles.sc1=sc1;
handles.sc2=sc2;
handles.l1=l1;
handles.l2=l2;

t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
figure
for i=1:N2;
subplot(2,N2,i)
imagesc(sc2(:,:,i,1));colormap(gray)
subplot(2,N2,i+N2)
plot(l2(:,i,1)), hline(0,'k--')
xlabel(t_var2)
subplot(2,N2,1)
title('Simplisma results of 1st pure images from mode 1','Position',[168 -5 9])
end

set(handles.Min_2p,'string',1);
set(handles.Max_2p,'string',q);
set(handles.Cur_2p,'string',1);
handles.type='Simplisma';
guidata(hObject,handles)

% --- Executes on button press in mcr.
function mcr_Callback(hObject, eventdata, handles)
% hObject    handle to mcr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

opt=questdlg('Do you want to apply nonnegativity to Concentrations?','MCR','Yes', 'No ', 'Yes');
if opt=='Yes'
   options.ccon='nonneg';
else
   options.ccon='none';
end

opt=questdlg('Do you want to apply nonnegativity to Spectra?','MCR','Yes', 'No ', 'Yes');
if opt=='Yes'
   options.scon='nonneg';
else
   options.scon='none';
end
options.display='off';
options.plots='final';

N1=handles.N1;
N2=handles.N2;
l1=handles.pca_l1;
data=handles.data;
[n,m,p,q]=size(data);
for i=1:q
images=data(:,:,:,i);
data1=reshape(images,[n*m p]);
xinit=l1(:,i,:);
c0=reshape(xinit,[N1 p]);
%c0=rand(N1,p);
[c1,s1] = als(data1,c0,options);
close(1)
close(2)
mcr_images1=reshape(c1,[n m N1]);
mcr_int1=s1';
for j=1:N1
    sc1(:,:,i,j)=mcr_images1(:,:,j);
    l1(:,i,j)=mcr_int1(:,j);
end
end
for i=1:N1
    data2=sc1(:,:,:,i);
    [n2,m2,p2]=size(data2);
    data3=reshape(data2,[n2*m2 p2]);
   c0=rand(N2,p2);
[c2,s2] = als(data3,c0,options);
close(1)
close(2)
mcr_images2=reshape(c2,[n2 m2 N2]);
mcr_int2=s2';
sc2(:,:,:,i)=mcr_images2;
l2(:,:,i)=mcr_int2;
end
handles.mcr_sc1=sc1;
handles.mcr_sc2=sc2;
handles.mcr_l1=l1;
handles.mcr_l2=l2;
handles.sc1=sc1;
handles.sc2=sc2;
handles.l1=l1;
handles.l2=l2;
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
figure
for i=1:N2;
subplot(2,N2,i)
imagesc(sc2(:,:,i,1));colormap(gray)
subplot(2,N2,i+N2)
plot(l2(:,i,1)), hline(0,'k--')
xlabel(t_var2)
subplot(2,N2,1)
title('MCR results of 1st component images from mode 1','Position',[168 -5 9])
end
set(handles.Min_2p,'string',1);
set(handles.Max_2p,'string',q);
set(handles.Cur_2p,'string',1);
handles.type='MCR';

guidata(hObject,handles)

% --- Executes on button press in Parafac.
function Parafac_Callback(hObject, eventdata, handles)
% hObject    handle to Parafac (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

data=handles.data;
[n,m,p,q]=size(data);
data2=reshape(data,[n*m, p,q]);
options=parafac('options');
options.constraints{1}.nonnegativity.algorithmforglobalmodel=1;
options.constraints{2}.nonnegativity.algorithmforglobalmodel=1;
options.constraints{3}.nonnegativity.algorithmforglobalmodel=1;
options.display='off';
options.plots='none';
N=inputdlg('Enter the number of components');  
N=str2double(N);
model=parafac(data2,N,options);
scores=reshape(model.loads{1},[n m N]);
loads1=model.loads{2};
loads2=model.loads{3};
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
H.Position=[521 187 262 521];
figure (H)
subplot(2,1,1)
plot(loads1)
hline(0,'k--')
axis tight
xlabel(t_var1)
subplot(2,1,2)
plot(loads2)
hline(0,'k--')
axis tight
xlabel(t_var2)
H.Position=[257 186 257 523];
figure(H)
if N<=3
    for i=1:N
    subplot(N,1,i)
    imagesc(scores(:,:,i));colormap(gray)
    end
elseif N==4
    for i=1:N
        subplot(2,2,i)
        imagesc(scores(:,:,i));colormap(gray)
    end
else
    [a,b,c]=size(scores);
im=reshape(scores,[a b 1 c]);
montage(im,colormap(gray))
end
handles.type3='parafac';
handles.parafac_model=model;
handles.scores_parafac=scores;
handles.loads1_parafac=loads1;
handles.loads2_parafac=loads2;
handles.scores3=scores;
handles.loads1_3=loads1;
handles.loads2_3=loads2;
guidata(hObject,handles)



function N_comp_Callback(hObject, eventdata, handles)
% hObject    handle to N_comp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of N_comp as text
%        str2double(get(hObject,'String')) returns contents of N_comp as a double
data=handles.data;
sc2=handles.sc2;
l2=handles.l2;
N=str2double(get(hObject,'String')) ;
N1=handles.N1;
N2=handles.N2;
figure
type=handles.type;
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
for i=1:N2;
subplot(2,N2,i)
imagesc(sc2(:,:,i,N));colormap(gray)
subplot(2,N2,i+N2)
plot(l2(:,i,N)), hline(0,'k--')
axis tight
xlabel(t_var2)
subplot(2,N2,1)
title(['The ', type ' results of component images #',  num2str(N),  ' from mode 1'],'Position',[168 -5 9])
end
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function N_comp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to N_comp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on slider movement.
function slider5_Callback(hObject, eventdata, handles)
% hObject    handle to slider5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

data=handles.data;
[n,m,p,q]=size(data);
sc1=handles.sc1;
l1=handles.l1;
N1=handles.N1;
step=1/q;
slider_step(1)=step;
slider_step(2)=step;
set(handles.slider5, 'SliderStep', slider_step, 'Max', q, 'Min',0)
i=get(hObject,'Value');
i=round(i);
if i==0
    i=1;
elseif i>=q
        i=q;
else i=i;
end
C=i;

set(handles.Cur_2p,'string',C);
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
type=handles.type;

figure
for i=1:N1;
subplot(2,N1,i)
imagesc(sc1(:,:,C,i));colormap(gray)
subplot(2,N1,i+N1)
plot(l1(:,C,i)), hline(0,'k--')
axis tight
xlabel(t_var1)
subplot(2,N1,1)
title(['The ',type,' results of images at ', t_var2, ' #' ,num2str(C)],'Position',[99 -5 9])
end

guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function slider5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function Cur_2p_Callback(hObject, eventdata, handles)
% hObject    handle to Cur_2p (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Cur_2p as text
%        str2double(get(hObject,'String')) returns contents of Cur_2p as a double

sc1=handles.sc1;
l1=handles.l1;
N1=handles.N1;
C=str2double(get(hObject,'String')) ;
set(handles.Cur_2p,'string',C);
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
type=handles.type;
figure
for i=1:N1;
subplot(2,N1,i)
imagesc(sc1(:,:,C,i));colormap(gray)
subplot(2,N1,i+N1)
plot(l1(:,C,i)), hline(0,'k--')
xlabel(t_var1)
subplot(2,N1,1)
title(['The ',type,' results of images at ', t_var2, ' #' ,num2str(C)],'Position',[99 -5 9])
end
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function Cur_2p_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Cur_2p (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end





function compN_Callback(hObject, eventdata, handles)
% hObject    handle to compN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of compN as text
%        str2double(get(hObject,'String')) returns contents of compN as a double
N1=handles.N1;
sc1=handles.sc1;
l1=handles.l1;
n=str2double(get(hObject,'String')) ;
sc=sc1(:,:,:,n);
[a,b,c]=size(sc);
im=reshape(sc,[a b 1 c]);
type=handles.type;
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
H.Position=[299 200 555 514];
figure(H)
montage(im,colormap(gray))
title(['The ', type, ' component images # ', num2str(n), ' from mode 1 as function of ',t_var2],'Position',[216 -7 9])
H2.Position=[860 204 407 510];
figure(H2)
plot(l1(:,:,n))
hline(0,'k--')
x=[1:1:c]';
text=num2str(x);
legend(text,2)
xlabel(t_var1)
title(['The ', type, ' loading # ', num2str(n), ' from mode 1 as function of ',t_var2])


% --- Executes during object creation, after setting all properties.
function compN_CreateFcn(hObject, eventdata, handles)
% hObject    handle to compN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in close.
function close_Callback(hObject, eventdata, handles)
% hObject    handle to close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all



% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
val = get(hObject,'Value');
switch val
case 2
handles.sc1=handles.pca_sc1;
handles.sc2=handles.pca_sc2;
handles.l1=handles.pca_l1;
handles.l2=handles.pca_l2;
handles.type='PCA';

case 3
handles.sc1=handles.simp_sc1;
handles.sc2=handles.simp_sc2;
handles.l1=handles.simp_l1;
handles.l2=handles.simp_l2;
handles.type='Simplisma';
case 4
handles.sc1=handles.mcr_sc1;
handles.sc2=handles.mcr_sc2;
handles.l1=handles.mcr_l1;
handles.l2=handles.mcr_l2;
handles.type='MCR'
case 5
handles.sc1=handles.maf_sc1;
handles.sc2=handles.maf_sc2;
handles.l1=handles.maf_l1;
handles.l2=handles.maf_l2;
handles.type='MAF'
end
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in mcr_3.
function mcr_3_Callback(hObject, eventdata, handles)
% hObject    handle to mcr_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[n,m,p,q]=size(data);
N=inputdlg('Enter the number of components');  
N=str2double(N);
[scores, load1, load2]=mcr_three_modes_pls(data,N);
handles.scores_mcr3=scores;
handles.loads1_mcr3=load1;
handles.loads2_mcr3=load2;
handles.type='MCR';
guidata(hObject,handles)


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[n,m,p,q]=size(data);
data2=reshape(data,[n*m, p,q]);
options=tucker('options');
options.constraints{1}.nonnegativity=1;
options.constraints{2}.nonnegativity=1;
options.constraints{3}.nonnegativity=1;
options.constraints{4}.nonnegativity=1;
options.display='off';
options.plots='none';
options.constraints{1}.orthogonal=0;
options.constraints{2}.orthogonal=0;
options.constraints{3}.orthogonal=0;
options.constraints{4}.orthogonal=0;
N=inputdlg('Enter the number of components');  
N=str2double(N);
model=tucker(data2,[N N N],options);
scores=reshape(model.loads{1},[n m N]);
loads1=model.loads{2};
loads2=model.loads{3};
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
H.Position=[521 187 262 521];
figure (H)
subplot(2,1,1)
plot(loads1)
hline(0,'k--')
axis tight
xlabel(t_var1)
subplot(2,1,2)
plot(loads2)
hline(0,'k--')
axis tight
xlabel(t_var2)
H.Position=[257 186 257 523];
figure(H)
if N<=3
    for i=1:N
    subplot(N,1,i)
    imagesc(scores(:,:,i));colormap(gray)
    end
elseif N==4
    for i=1:N
        subplot(2,2,i)
        imagesc(scores(:,:,i));colormap(gray)
    end
else
    [a,b,c]=size(scores);
im=reshape(scores,[a b 1 c]);
montage(im,colormap(gray))
end
handles.type3='tucker';
handles.model_tucker=model;
handles.scores_tucker=scores;
handles.loads1_tucker=loads1;
handles.loads2_tucker=loads2;
handles.scores3=scores;
handles.loads1_3=loads1;
handles.loads2_3=loads2;
guidata(hObject,handles)



% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --------------------------------------------------------------------
function save_mat_Callback(hObject, eventdata, handles)
% hObject    handle to save_mat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
svdlgpls(data,'save data into mat file or workspace')
% --------------------------------------------------------------------
function save_pca_Callback(hObject, eventdata, handles)
% hObject    handle to save_pca (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

pca_model.sc1=handles.pca_sc1;
pca_model.sc2=handles.pca_sc2;
pca_model.l1=handles.pca_l1;
pca_model.l2=handles.pca_l2;
svdlgpls(pca_model,'save PCA model into mat file or workspace')

% --------------------------------------------------------------------
function save_simplisma_Callback(hObject, eventdata, handles)
% hObject    handle to save_simplisma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

simp_model.sc1=handles.simp_sc1;
simp_model.sc2=handles.simp_sc2;
simp_model.l1=handles.simp_l1;
simp_model.l2=handles.simp_l2;
svdlgpls(simp_model,'save Simplisma model into mat file or workspace')

% --------------------------------------------------------------------
function save_mcr_Callback(hObject, eventdata, handles)
% hObject    handle to save_mcr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mcr_model.sc1=handles.mcr_sc1;
mcr_model.sc2=handles.mcr_sc2;
mcr_model.l1=handles.mcr_l1;
mcr_model.l2=handles.mcr_l2;
svdlgpls(mcr_model,'save MCR model into mat file or workspace')

% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --------------------------------------------------------------------
function shrink_Callback(hObject, eventdata, handles)
% hObject    handle to shrink (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
N=inputdlg('Enter N times to shrink the image');  
N=str2double(N);
[n,m,p,q]=size(data);
data_res(:,:,1,1) = imresize(data(:,:,1,1),1/N,'bicubic');
data_res(:,:,p,q) = imresize(data(:,:,p,q),1/N,'bicubic');
for i=1:p
    for j=1:q
        data_res(:,:,i,j) = imresize(data(:,:,i,j),1/N,'bicubic');
    end
end
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data_res(:,:,1,1)), colormap(gray)
handles.data=data_res;
handles.data_res=data_res;
guidata(hObject,handles)

    


% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --------------------------------------------------------------------
function undo_Callback(hObject, eventdata, handles)
% hObject    handle to undo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.or_data;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,1,1)), colormap(gray)
handles.data=data;
guidata(hObject,handles)


% --------------------------------------------------------------------
function crop_Callback(hObject, eventdata, handles)
% hObject    handle to crop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[n,m,p,q]=size(data);
figure(1)
P=round(p/2);
Q=round(q/2);
[a,rect]=imcrop(uint8(data(:,:,P,Q)));
data_crop(:,:,1,1)=imcrop(data(:,:,1,1),rect);
data_crop(:,:,p,q)=imcrop(data(:,:,p,q),rect);
for i=1:p
    for j=1:q
        data_crop(:,:,i,j)=imcrop(data(:,:,i,j),rect);
    end
end
close(1)
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data_crop(:,:,P,Q)), colormap(gray)
handles.data=data_crop;
handles.data_crop=data_crop;
guidata(hObject,handles)


% --------------------------------------------------------------------
function adjust_max_Callback(hObject, eventdata, handles)
% hObject    handle to adjust_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[m,n,p,q]=size(data);
for i=1:p
    for j=1:q
        a(i,j)=max(max(data(:,:,i,j)));
    end
end
y=max(max(a))

k=y/255;
data_div(:,:,1,1)=data(:,:,1,1)./k;
data_div(:,:,p,q)=data(:,:,p,q)./k;
for i=1:p
    for j=1:q
        data_div(:,:,i,j)=data(:,:,i,j)./k;
    end
end

iptsetpref('ImshowAxesVisible', 'on')
imagesc(data_div(:,:,1,1)), colormap(gray)
handles.data=data_div;
handles.data_div=data_div;
guidata(hObject,handles);



% --------------------------------------------------------------------
function open_images_Callback(hObject, eventdata, handles)
% hObject    handle to open_images (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname]=uigetfiles('*.*','Open images');
cd(pathname)
[N,M]=size(filename);
a=str2double(inputdlg('Enter number of Channels'));  
b=str2double(inputdlg('Enter number of depth slices'));  
M1=a*b;
if M1==M
else 
    HANDLE = errordlg('The number of images opened does not match # of channels and depths','Opening Images');
end
image=imread(char(filename(:,1)));
data(:,:,1,1)=double(image);
data(:,:,a,b)=double(image);
for i=1:b
    k=i*a;
    for j=(k-a+1):k
    image=imread(char(filename(:,j)));
    r=j-(i-1)*a;
    data(:,:,r,i)=double(image);
    end
end
handles.data=data;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,1,1)), colormap(gray)
[n,m,p,q]=size(data);
set(handles.Min_1,'string',1);
set(handles.Max_1,'string',p);
set(handles.Cur_1,'string',1);
set(handles.Min_2,'string',1);
set(handles.Max_2,'string',q);
set(handles.Cur_2,'string',1);
prompt={'Variable 1:','Variable2:'};
def={'Wavelength','Depth'};
dlgTitle='Enter the title of variables';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
set(handles.title_var1,'string',answer{1});
set(handles.title_var2,'string',answer{2});
handles.tit_var1=answer{1};
handles.tit_var2=answer{2};
handles.C1=1;
handles.C2=1;
guidata(hObject,handles)


% --------------------------------------------------------------------
function flip_mode_Callback(hObject, eventdata, handles)
% hObject    handle to flip_mode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[n,m,p,q]=size(data);

for i=1:p
    data_flip(:,:,:,i)=data(:,:,i,:);
end
handles.data=data_flip;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data_flip(:,:,1,1)), colormap(gray)
[n,m,p,q]=size(data_flip);
set(handles.Min_1,'string',1);
set(handles.Max_1,'string',p);
set(handles.Cur_1,'string',1);
set(handles.Min_2,'string',1);
set(handles.Max_2,'string',q);
set(handles.Cur_2,'string',1);
handles.C1=1;
handles.C2=1;
t_var2=handles.tit_var1;
t_var1=handles.tit_var2;
set(handles.title_var1,'string',t_var1);
set(handles.title_var2,'string',t_var2);
handles.tit_var1=t_var1;
handles.tit_var2=t_var2;
guidata(hObject,handles)


% --------------------------------------------------------------------
function Profile_1_Callback(hObject, eventdata, handles)
% hObject    handle to Profile_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[n,m,p,q]=size(data);
C1=handles.C1;
C2=handles.C2;
images=data(:,:,:,C2);
profiles(images,'1')  

% --------------------------------------------------------------------
function profile_2_Callback(hObject, eventdata, handles)
% hObject    handle to profile_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.data;
[n,m,p,q]=size(data);
C1=handles.C1;
C2=handles.C2;
images=data(:,:,C1,:);
images=reshape(images,[n,m,q]);
profiles(images,'1')  

% --------------------------------------------------------------------
function Analyze_Callback(hObject, eventdata, handles)
% hObject    handle to Analyze (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --------------------------------------------------------------------
function save_parafac_Callback(hObject, eventdata, handles)
% hObject    handle to save_parafac (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
parafac_model=handles.parafac_model;
svdlgpls(parafac_model,'save Parafac model into mat file or workspace')

% --------------------------------------------------------------------
function save_tucker_Callback(hObject, eventdata, handles)
% hObject    handle to save_tucker (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
model_tucker=handles.model_tucker;
svdlgpls(model_tucker,'save Parafac model into mat file or workspace')


function Ncomp_3way_Callback(hObject, eventdata, handles)
% hObject    handle to Ncomp_3way (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Ncomp_3way as text
%        str2double(get(hObject,'String')) returns contents of Ncomp_3way as a double

type=handles.type3;
scores=handles.scores3;
loads1=handles.loads1_3;
loads2=handles.loads2_3;
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;
N=str2double(get(hObject,'String')) ;
H.Position=[521 187 262 521];
figure (H)
subplot(3,1,1)
imagesc(scores(:,:,N));colormap(gray)
subplot(3,1,2)
plot(loads1(:,N))
axis tight
hline(0,'k--')
xlabel(t_var1)
subplot(3,1,3)
plot(loads2(:,N))
hline(0,'k--')
axis tight
xlabel(t_var2)
subplot(3,1,1)
title(['The ', type, ' results for component #', num2str(N)])



% --- Executes during object creation, after setting all properties.
function Ncomp_3way_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Ncomp_3way (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in disp_res_3way.
function disp_res_3way_Callback(hObject, eventdata, handles)
% hObject    handle to disp_res_3way (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns disp_res_3way contents as cell array
%        contents{get(hObject,'Value')} returns selected item from disp_res_3way
val = get(hObject,'Value');
switch val
case 2
scores=handles.scores_parafac;
loads1=handles.loads1_parafac;
loads2=handles.loads2_parafac;
handles.type3='parafac';

case 3
scores=handles.scores_tucker;
loads1=handles.loads1_tucker;
loads2=handles.loads2_tucker;
handles.type3='tucker';

case 4
scores=handles.scores_mcr3;
loads1=handles.loads1_mcr3;
loads2=handles.loads2_mcr3;
handles.type3='MCR';

case 5
scores=handles.scores_MAF;
loads1=handles.loads1_MAF;
loads2=handles.loads2_MAF;
handles.type3='MAF';
end

handles.scores3=scores;
handles.loads1_3=loads1;
handles.loads2_3=loads2;

guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function disp_res_3way_CreateFcn(hObject, eventdata, handles)
% hObject    handle to disp_res_3way (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --------------------------------------------------------------------
function save_3MCR_Callback(hObject, eventdata, handles)
% hObject    handle to save_3MCR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

MCR3model.scores=handles.scores_mcr3;
MCR3model.loads1=handles.loads1_mcr3;
MCR3model.loads2=handles.loads2_mcr3;
svdlgpls(MCR3model,'save MCR 3way model into mat file or workspace')


% --- Executes on button press in MAF.
function MAF_Callback(hObject, eventdata, handles)
% hObject    handle to MAF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

data=handles.data;
[n,m,p,q]=size(data);
N=inputdlg('Enter the number of components');  
Ncomp=str2double(N);

[n,m,p,q]=size(data);
data2=reshape(data,[n*m, p,q]);
data_row=reshape(data2,[n*m p*q]); 
[scores_row,loads_row,mn,ssq] = maf(data_row);
scores=scores_row(:,1:Ncomp);
scores=reshape(scores,[n m Ncomp]);

data_column1=reshape(data2, [n*m*q p]);
[scores_column1,loads_column1,mn,ssq] = maf(data_column1);
load1=loads_column1(:,1:Ncomp);
for i=1:p
    data_flip(:,:,:,i)=data(:,:,i,:);
  end
[n1,m1,p1,q1]=size(data_flip);
data2_f=reshape(data_flip,[n1*m1, p1,q1]);
data_column2=reshape(data2_f,[n1*m1*q1 p1]);
[scores_column2,loads_column2,mn,ssq] = maf(data_column2);
load2=loads_column2(:,1:Ncomp);

H.Position=[761 43 285 577];
figure(H)
subplot(2,1,1)
plot(load1)
xlabel('variable1')
subplot(2,1,2)
plot(load2)
xlabel('variable2')
subplot(2,1,1)
title(['MAF results'])

H.Position=[479 43 276 577];
figure(H)
[a,b,c]=size(scores);
for i=1:c
    subplot(c,1,i)
    imagesc(scores(:,:,i)), colormap (gray)
end
subplot(c,1,1)
title(['MAF results'])

handles.type4='MAF';
handles.scores_MAF=scores;
handles.loads1_MAF=load1;
handles.loads2_MAF=load2;
guidata(hObject,handles)




% --------------------------------------------------------------------
function save_maf_Callback(hObject, eventdata, handles)
% hObject    handle to save_maf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

MAFmodel.scores=handles.scores_MAF;
MAFmodel.loads1=handles.loads1_MAF;
MAFmodel.loads2=handles.loads2_MAF;
svdlgpls(MAFmodel,'save MAF 3way model into mat file or workspace')




% --- Executes on button press in MAF2.
function MAF2_Callback(hObject, eventdata, handles)
% hObject    handle to MAF2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

N1=handles.N1;
N2=handles.N2;
scal=handles.scal;
data=handles.data;
h = waitbar(0,'Please wait while MAF is performed...');
[n,m,p,q]=size(data);
for i=1:q
images=data(:,:,:,i);
[scores, loads, mn,ssq] = maf(images);
for j=1:N1
    sc1(:,:,i,j)=scores(:,:,j);
    l1(:,i,j)=loads(:,j);
end
end
for i=1:N1
    [scores2, loads2, mn,ssq] = maf(sc1(:,:,:,i));
    sc2(:,:,:,i)=scores2;
    l2(:,:,i)=loads2;
end
close(h)
handles.maf_sc1=sc1;
handles.maf_sc2=sc2;
handles.maf_l1=l1;
handles.maf_l2=l2;
handles.sc1=sc1;
handles.sc2=sc2;
handles.l1=l1;
handles.l2=l2;
t_var1=handles.tit_var1;
t_var2=handles.tit_var2;


figure
for i=1:N2;
subplot(2,N2,i)
imagesc(sc2(:,:,i,1));colormap(gray)
subplot(2,N2,i+N2)
plot(l2(:,i,1)), hline(0,'k--')
axis tight
xlabel(t_var2)
subplot(2,N2,1)
title('MAF results of 1st PC score images from mode 1','Position',[168 -5 9])
end

set(handles.Min_2p,'string',1);
set(handles.Max_2p,'string',q);
set(handles.Cur_2p,'string',1);
handles.type='MAF';
guidata(hObject,handles)


% --- Executes on button press in classification.
function classification_Callback(hObject, eventdata, handles)
% hObject    handle to classification (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

data4=handles.data;
method=questdlg('Select classification method', 'Classification','K means','Fuzzy c','MLE    ', 'K means');
 if method=='K means'
     N = inputdlg('Enter number of classes');
        n=str2double(N);
        h = waitbar(0,'Please wait while classification is performed...');
        [a,b,c,d]=size(data4);
        for k=1:d
           image=data4(:,:,:,k);
           [m,n,p]=size(image);
           Data=reshape(image, [m*n p]);
                     classes=dcKMeans(Data,n);
           class4(:,:,k)=reshape(classes,[m n]);
        end
        close(h)
        elseif method=='Fuzzy c'
        N = inputdlg('Enter number of classes');
        n=str2double(N);
        h = waitbar(0,'Please wait while classification is performed...');
        [a,b,c,d]=size(data4);
        for k=1:d
           image=data4(:,:,:,k);
           [m,n,p]=size(image);
           Data=reshape(image, [m*n p]);
           [classes,U] = dcFuzzy(Data,n);
           class4(:,:,k)=reshape(classes,[m n]);
                close(h)
        end
 else
                    
    
opt=questdlg('Which image to use for ROI selection?','ROI selection','Original', 'Model   ', 'Original');
if opt=='Original'
    prompt={'Variable 1:','Variable2:'};
    def={'1','1'};
    dlgTitle='Enter the # of image in Variable 1 and 2 to use';
    lineNo=1;
    answer=inputdlg(prompt,dlgTitle,lineNo,def);
    imageROI=data4(:,:,str2double(answer{1}),str2double(answer{2}));
else
    opt2=questdlg('Which model to use for ROI selection?','ROI selection','MCR    ', 'Tucker ', 'Parafac', 'MCR    ');
    if opt2=='MCR    '
        N = inputdlg('Enter component number to use');
        n=str2double(N);
        imageROI=uint8(handles.scores_mcr3(:,:,n));
    elseif opt2=='Tucker '
        N = inputdlg('Enter component number to use');
        n=str2double(N);    
        imageROI=uint8(handles.scores_tucker(:,:,n));
    else
        N = inputdlg('Enter component number to use');
        n=str2double(N); 
        imageROI=uint8(handles.scores_parafac(:,:,n));
    end
end
N = inputdlg('How many training ROIs you want to spesify?');
n=str2double(N);
for i=1:n;
          [J,BW] = roifill(imageROI);
          roi(:,:,i)=BW;
end

[a,b,c,d]=size(data4);
for k=1:d
im=data4(:,:,:,k);
[a,b,n]=size(roi);

for i=1:n;
    [I,J] = find(roi(:,:,i)==1); %find indices of all pixels within ROIs
    [m(i),p(i)]=size(I);
    clear v
    for j=1:m(i)
    v(j,:)=im(I(j),J(j),:); % creating ROI matrices containing pixels within ROIs
    end
    M(i,:)=mean(v); % calculating mean vectors for each ROI matrix 
    siz = [m(i) 1]; 
    clear M1
    M1=M(i,:);
    [p,q] = size(M1); 
    mind1 = (1:p)'; nind1 = (1:q)'; 
    mind1 = mind1(:,ones(1,siz(1)));
    nind1 = nind1(:,ones(1,siz(2)));
    clear Me
    Me = M1(mind1,nind1); % creating ROI mean matrix - each column has the same vector of means
    clear Vc
    Vc = v - Me; % Remove the mean matrix from the ROI matrix data
    clear cov
    cov = Vc' * Vc / (m(i)-1); % calculating covariance matrices for each ROI
    Det(i)=det(cov); % calculating the determinant of the covariance matrix for each ROI
    Inv(:,:,i)=(inv(cov)); %calculating invrse of covariance matrix for each ROI
end

% calculating discriminant function for each pixel
for i1=1:a
    for j1=1:b
        x=im(i1,j1,:);
        x=reshape(x,[1 q]); % extracting pixel value or vector
        for i2=1:n;
            g(i2) = - log(Det(i2))-(x'-M(i2,:)')' *Inv(:,:,i2)*(x'-M(i2,:)'); % ln(det(Cov)-(x-Mean)T*inv(Cov)*(x-Mean)
        end
        G=sum(g); % sum of all discriminant functions
        for i=1:n
            P(i)=g(i)/G; % calculating probability vector

        end
        [r,s]=max(P); % determining the max probbality - index s =class number

        % Class assignment
        for i=1:n
            A(i)=250/(n-i+1); % claculating vector of colors to assing (equally spaced numbers between 0 and 250)
            if s==i
             class(i1,j1)=A(i); %class assignment
         end
        end         
     end
end
class4(:,:,k)=class;
end
end
 
handles.class=class4;
guidata(hObject,handles)
[a,b,c]=size(class4);
H.Position=[421 130 1023 537];
figure(H);
subplot(2,2,1)
imagesc(class4(:,:,1),[0 260])
subplot(2,2,2)
imagesc(class4(:,:,round(c/4)+1),[0 260])
subplot(2,2,3)
imagesc(class4(:,:,round(c/4*3)),[0 260])
subplot(2,2,4)
imagesc(class4(:,:,round(c)),[0 260])


      


% --------------------------------------------------------------------
function save_class_MLE_Callback(hObject, eventdata, handles)
% hObject    handle to save_class_MLE (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

class4=handles.class;
svdlgpls(class4,'save MLE classification model into mat file or workspace')

Contact us at files@mathworks.com