function varargout = axis_data_control(varargin)
% AXIS_DATA_CONTROL Application M-file for axis_data_control.fig
%    FIG = AXIS_DATA_CONTROL launch axis_data_control GUI.
%    AXIS_DATA_CONTROL('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.0 04-Feb-2002 12:45:43

if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'reuse');

	% Use system color scheme for figure:
	set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
	guidata(fig, handles);

	if nargout > 0
		varargout{1} = fig;
	end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		if (nargout)
			[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
		else
			feval(varargin{:}); % FEVAL switchyard
		end
	catch
		disp(lasterr);
	end

end


%| ABOUT CALLBACKS:
%| GUIDE automatically appends subfunction prototypes to this file, and 
%| sets objects' callback properties to call them through the FEVAL 
%| switchyard above. This comment describes that mechanism.
%|
%| Each callback subfunction declaration has the following form:
%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)
%|
%| The subfunction name is composed using the object's Tag and the 
%| callback type separated by '_', e.g. 'slider2_Callback',
%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.
%|
%| H is the callback object's handle (obtained using GCBO).
%|
%| EVENTDATA is empty, but reserved for future use.
%|
%| HANDLES is a structure containing handles of components in GUI using
%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This
%| structure is created at GUI startup using GUIHANDLES and stored in
%| the figure's application data using GUIDATA. A copy of the structure
%| is passed to each callback.  You can store additional information in
%| this structure at GUI startup, and you can change the structure
%| during callbacks.  Call guidata(h, handles) after changing your
%| copy to replace the stored original so that subsequent callbacks see
%| the updates. Type "help guihandles" and "help guidata" for more
%| information.
%|
%| VARARGIN contains any extra arguments you have passed to the
%| callback. Specify the extra arguments by editing the callback
%| property in the inspector. By default, GUIDE sets the property to:
%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))
%| Add any extra arguments after the last argument, before the final
%| closing parenthesis.

% --------------------------------------------------------------------
function varargout = plot_data_Callback(h, eventdata, handles, varargin)
Fs = handles.Fs;
y = handles.y;
% set the current axes that you wish to plot in
axes(handles.axes2);
handles.h_line = plot(1/Fs:1/Fs:length(y)/Fs,y);

%%set(handles.h_line,'buttondownfcn', ...
%%    'axis_data_control(''axes2_ButtonDownFcn'',handles.h_line,[],guidata(handles.h_line))')

% set the context menu
set(handles.h_line, 'uicontextmenu', handles.line_context);

% save original axis limits
handles.Xlim_orig = get(handles.axes2, 'Xlim');
handles.Ylim_orig = get(handles.axes2, 'Ylim');
% update the current axis limit values
set(handles.xmin, 'string', num2str(handles.Xlim_orig(1)));
set(handles.xmax, 'string', num2str(handles.Xlim_orig(2)));
set(handles.ymin, 'string', num2str(handles.Ylim_orig(1)));
set(handles.ymax, 'string', num2str(handles.Ylim_orig(2)));
% update slider information
set(handles.freq_slider, 'value', handles.Fs)
set(handles.freq_value,'string', num2str(handles.Fs))
% incorporate the new fields to GUIDATA
guidata(h,handles)

% --------------------------------------------------------------------
function varargout = reset_axis_Callback(h, eventdata, handles, varargin)
set(handles.axes2, 'Xlim', handles.Xlim_orig, ...
    'Ylim', handles.Ylim_orig);
% set the editable text boxes with the appropriate value
set(handles.xmin, 'string', num2str(handles.Xlim_orig(1)));
set(handles.xmax, 'string', num2str(handles.Xlim_orig(2)));
set(handles.ymin, 'string', num2str(handles.Ylim_orig(1)));
set(handles.ymax, 'string', num2str(handles.Ylim_orig(2)));

% --------------------------------------------------------------------
function varargout = zoom_Callback(h, eventdata, handles, varargin)
% set the axes to be current and turn on/off ZOOM
axes(handles.axes2);
if get(h,'value') == 1
    zoom on
else
    zoom off
    xlim = get(handles.axes2, 'xlim');
    ylim = get(handles.axes2, 'ylim');
    set(handles.xmin,'string', num2str(xlim(1)));
    set(handles.xmax,'string', num2str(xlim(2)));
    set(handles.ymin,'string', num2str(ylim(1)));
    set(handles.ymax,'string', num2str(ylim(2)));
end

% --------------------------------------------------------------------
function varargout = mat_file_Callback(h, eventdata, handles, varargin)
% save the present working directory
% and change the appropriate directory
temp = pwd;
cd D:\Applications\matlab6p1\toolbox\signal\signal

% load in the MAT-file
[fn,pn]= uigetfile('*.mat');
load([pn,fn]);

% change back to the original directory
% and create new fields for the loaded data
cd(temp)
handles.Fs = Fs;
handles.y = mtlb;
guidata(h, handles);

% --------------------------------------------------------------------
function varargout = line_dashed_Callback(h, eventdata, handles, varargin)
set(handles.h_line, 'LineStyle', '--')

% --------------------------------------------------------------------
function varargout = line_dotted_Callback(h, eventdata, handles, varargin)
set(handles.h_line, 'LineStyle', ':')

% --------------------------------------------------------------------
function varargout = line_solid_Callback(h, eventdata, handles, varargin)
set(handles.h_line, 'LineStyle', '-')

% --------------------------------------------------------------------
function varargout = line_reset_Callback(h, eventdata, handles, varargin)
% Same as reset_axis_Callback
set(handles.axes2, 'Xlim', handles.Xlim_orig, ...
    'Ylim', handles.Ylim_orig);
set(handles.xmin, 'string', num2str(handles.Xlim_orig(1)));
set(handles.xmax, 'string', num2str(handles.Xlim_orig(2)));
set(handles.ymin, 'string', num2str(handles.Xlim_orig(1)));
set(handles.ymax, 'string', num2str(handles.Xlim_orig(2)));

% --------------------------------------------------------------------
function varargout = xmin_Callback(h, eventdata, handles, varargin)
% Change the xmin value to that which is entered in the text box
xmin = str2num(get(h,'string'));
current_xlim = get(handles.axes2, 'Xlim');

set(handles.axes2, 'Xlim', [xmin, current_xlim(2)]);

% --------------------------------------------------------------------
function varargout = xmax_Callback(h, eventdata, handles, varargin)
xmax = str2num(get(h,'string'));
current_xlim = get(handles.axes2, 'Xlim');

set(handles.axes2, 'Xlim', [current_xlim(1), xmax]);

% --------------------------------------------------------------------
function varargout = ymin_Callback(h, eventdata, handles, varargin)
ymin = str2num(get(h,'string'));
current_ylim = get(handles.axes2, 'Ylim');

set(handles.axes2, 'Ylim', [ymin, current_ylim(2)]);

% --------------------------------------------------------------------
function varargout = ymax_Callback(h, eventdata, handles, varargin)
ymax = str2num(get(h,'string'));
current_ylim = get(handles.axes2, 'Ylim');

set(handles.axes2, 'Ylim', [current_ylim(1), ymax]);

% --------------------------------------------------------------------
% does not work right now... I am not sure what is wrong with it. The 
% code is right but the ButtonDownFcn is not functioning in the way
% that I am expecting.
function varargout = axes2_ButtonDownFcn(h, eventdata, handles, varargin)
disp('in button down fcn')
get(handles.zoom, 'value')
if get(handles.zoom, 'value') == get(handles.zoom, 'max')
    disp('in if')
    xlim = get(handles.axes2, 'xlim');
    ylim = get(handles.axes2, 'ylim');
    set(handles.xmin,'string', num2str(xlim(1)));
    set(handles.xmax,'string', num2str(xlim(2)));
    set(handles.ymin,'string', num2str(ylim(1)));
    set(handles.ymax,'string', num2str(ylim(2)));
end

% --------------------------------------------------------------------
function varargout = freq_slider_Callback(h, eventdata, handles, varargin)
% Change the sampling freq with that of the slider value
handles.Fs = get(h,'value');
guidata(h,handles)
set(handles.freq_value, 'string', num2str(get(h,'value')))



% --------------------------------------------------------------------
function varargout = play_data_Callback(h, eventdata, handles, varargin)
% play only the data that is shown in the current axes
y = handles.y;
xdata = get(handles.h_line, 'xdata');
current_xlim = get(handles.axes2, 'Xlim');
% Obtain only the data shown on the axes
y = y(xdata>=current_xlim(1) & xdata<=current_xlim(2));
sound(y,handles.Fs)

% --------------------------------------------------------------------
function varargout = Untitled_1_Callback(h, eventdata, handles, varargin)

% --------------------------------------------------------------------
function varargout = line_context_Callback(h, eventdata, handles, varargin)