function varargout = varviewer3D(varargin)
% VARVIEWER3D MATLAB code for varviewer3D.fig
%      VARVIEWER3D, by itself, creates a new VARVIEWER3D or raises the existing
%      singleton*.
%
%      H = VARVIEWER3D returns the handle to a new VARVIEWER3D or the handle to
%      the existing singleton*.
%
%      VARVIEWER3D('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in VARVIEWER3D.M with the given input arguments.
%
%      VARVIEWER3D('Property','Value',...) creates a new VARVIEWER3D or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before varviewer3D_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to varviewer3D_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 varviewer3D

% Last Modified by GUIDE v2.5 06-Feb-2013 10:46:47

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

% Choose default command line output for varviewer3D
handles.output = hObject;
if ~isnumeric(varargin{1}) || numel(size(varargin{1})) ~= 3
    close(handles.figure1)
    error('varviewer3D should be called with a numeric 3D input');
end
handles.data = varargin{1};
% Update handles structure
guidata(hObject, handles);
set(handles.slider1, 'Min', 1);
set(handles.slider1, 'Max', size(handles.data, 3));
set(handles.slider1, 'Value', 1);
set(handles.slider1, 'SliderStep', repmat(1/(size(handles.data,3)-1),1,2));
layer = handles.data(:,:,1);
assignin('base', 'layer', layer)
openvar('layer');

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


% --- Outputs from this function are returned to the command line.
function varargout = varviewer3D_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
idx = round(get(handles.slider1, 'Value'));
layer = handles.data(:,:,idx);
assignin('base', 'layer', layer)
openvar('layer')


% --- 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
