function varargout = gui_mixture(varargin)
% GUI_MIXTURE M-file for gui_mixture.fig
%      GUI_MIXTURE, by itself, creates a new GUI_MIXTURE or raises the existing
%      singleton*.
%
%      H = GUI_MIXTURE returns the handle to a new GUI_MIXTURE or the handle to
%      the existing singleton*.
%
%      GUI_MIXTURE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_MIXTURE.M with the given input arguments.
%
%      GUI_MIXTURE('Property','Value',...) creates a new GUI_MIXTURE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_mixture_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_mixture_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_mixture

% Last Modified by GUIDE v2.5 15-May-2008 01:24:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_mixture_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_mixture_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 gui_mixture is made visible.
function gui_mixture_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 gui_mixture (see VARARGIN)


X = varargin{1};
C = varargin{2};

[m n] = size(X);
if n ~= 2, error('Demo only works in 2D.'); end

handles.K = size(C.mu{1},1);
handles.X = X;
handles.mu = C.mu;
handles.c = C.c;
handles.J = C.J;
handles.iter = length(C.c);
handles.h = zeros(1,handles.K);
handles.hm = 0;
handles.hj = 0;

handles


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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gui_mixture_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 slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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

curpos = get(handles.slider,'Value');
maxpos = get(handles.slider,'Max');
minpos = get(handles.slider,'Min');

iter = ceil((curpos-minpos)/(maxpos-minpos)*handles.iter);

set(handles.hm,'xdata',handles.mu{iter}(:,1),'ydata',handles.mu{iter}(:,2));
set(handles.hj,'xdata',iter,'ydata',handles.J(iter));
for k = 1:K
  i = find(handles.c{iter}==k);
  nk = length(i);
  xx = reshape([handles.X(i,1) handles.mu{iter}(k*ones(1,nk),1)]',1,2*nk);
  yy = reshape([handles.X(i,2) handles.mu{iter}(k*ones(1,nk),2)]',1,2*nk);
  set(handles.h(k),'xdata',xx,'ydata',yy);
end
drawnow



% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

handles.slider = hObject;
guidata(hObject,handles);

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(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)

curpos = get(handles.slider,'Value');
maxpos = get(handles.slider,'Max');
minpos = get(handles.slider,'Min');

iter = ceil((curpos-minpos)/(maxpos-minpos)*handles.iter);

plot(handles.J,'b-');
hold on
handles.hj = plot(1,handles.J(1),'*','markersize',10,'linewidth',3);
hold off


set(handles.hm,'xdata',handles.mu{iter}(:,1),'ydata',handles.mu{iter}(:,2));
set(handles.hj,'xdata',iter,'ydata',handles.J(iter));
for k = 1:K
  i = find(handles.c{iter}==k);
  nk = length(i);
  xx = reshape([handles.X(i,1) handles.mu{iter}(k*ones(1,nk),1)]',1,2*nk);
  yy = reshape([handles.X(i,2) handles.mu{iter}(k*ones(1,nk),2)]',1,2*nk);
  set(handles.h(k),'xdata',xx,'ydata',yy);
end
drawnow


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

% Hint: place code in OpeningFcn to populate axes1



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

% Hint: place code in OpeningFcn to populate axes2

handles.a2 = hObject;
guidata(hObject, handles);
