function varargout = plotting_data_gui(varargin)
% PLOTTING_DATA_GUI M-file for plotting_data_gui.fig
%      PLOTTING_DATA_GUI, by itself, creates a new PLOTTING_DATA_GUI or raises the existing
%      singleton*.
%
%      H = PLOTTING_DATA_GUI returns the handle to a new PLOTTING_DATA_GUI or the handle to
%      the existing singleton*.
%
%      PLOTTING_DATA_GUI('Property','Value',...) creates a new PLOTTING_DATA_GUI using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to plotting_data_gui_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      PLOTTING_DATA_GUI('CALLBACK') and PLOTTING_DATA_GUI('CALLBACK',hObject,...) call the
%      local function named CALLBACK in PLOTTING_DATA_GUI.M with the given input
%      arguments.
%
%      *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 plotting_data_gui

% Last Modified by GUIDE v2.5 15-May-2002 12:58:00

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @plotting_data_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @plotting_data_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 plotting_data_gui is made visible.
function plotting_data_gui_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

% Create a guidata field that has all handles associated with defining the X and
% Y values.
handles.define_xy_objs = [handles.x_start, handles.x_stop, handles.x_num_val, ...
        handles.y_value, ...
        handles.text1, handles.text2, handles.text3, handles.text4, handles.text5];
% as a starting point the define_xy checkbox is selected.
set(handles.define_xy, 'value', 1);
set(handles.define_xy_objs,...
    'visible','on',...
    'enable', 'on');

% Create a guidata field that has all handles associated with loading the X and Y
% values from a MAT-file.
handles.load_xy_objs = [handles.text6, handles.text8, handles.text9,...
        handles.load_fname, handles.browse_fname,...
        handles.x_var_list, handles.y_var_list];
% as a starting point the load_xy checkbox and its options are unselected.
set(handles.load_xy, 'value', 0);
set(handles.load_xy_objs,...
    'visible','off',...
    'enable', 'off');


% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = plotting_data_gui_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 button press in plot_line.
function plot_line_Callback(hObject, eventdata, handles)
% hObject    handle to plot_line (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hold off
set(handles.coord_text, 'string', '');

if get(handles.define_xy, 'value')
    x_stop = str2num(get(handles.x_stop,'string'));
    x_start = str2num(get(handles.x_start,'string'));
    x_num_val = round( str2num(get(handles.x_num_val,'string')) );
    if x_stop > x_start
        if x_num_val > 0
            handles.x = linspace(x_start, x_stop, x_num_val);
            if ~isempty(get(handles.y_value, 'string'))
                if isfield(handles, 'line')
                    ln_color = get(handles.line, 'color');
                    ln_style = get(handles.line, 'linestyle');
                    ln_marker = get(handles.line, 'marker');
                else
                    ln_color = 'b';
                    ln_style = '-';
                    ln_marker = 'none';
                end % isfield(handles, 'line')
            	y_string = get(handles.y_value,'string');
                x = handles.x;
                try 
                    eval(['handles.y=',y_string, ';']);
                catch
                    dialog_gui('title','Error Message',...
                        'string','The equation for f(x) is not valid.');
                    return
                end
                handles.line = plot(handles.x, handles.y);
                xlabel('x data')
                ylabel(['f(x) = ',y_string]);
                % set(get(handles.my_axes,'xlabel'), 'string', 'test xlabel')
                % set(get(handles.my_axes,'ylabel'),'string', 'ylabel test')
                set(handles.line, 'color', ln_color, 'linestyle', ln_style, 'marker', ln_marker);
                guidata(hObject,handles);  
            else 
                dialog_gui('title','Error Message',...
                    'string','Please enter in an equation for f(x).');
            end % ~isempty(get(handles.y_value, 'string'))
        else
            dialog_gui('title','Error Message',...
                'string',['How many values between ',...
                num2str(x_start), ' & ', num2str(x_stop), '?']);
        end % x_num_val > 0
    else
        dialog_gui('title','Error Message',...
            'string','Please enter a valid start/stop value of X.');
    end % x_stop > x_start
    
else  
    if ( get(handles.load_xy, 'value') == 1 ) & isfield(handles, 'filename') & exist(handles.filename,'file') 
        index = get([handles.x_var_list,handles.y_var_list], 'value');
        % note that they are the same for both var_lists 
        var_list = get(handles.x_var_list, 'string');
        x_var = var_list{index{1}};
        y_var = var_list{index{2}};
        data = load(handles.filename, x_var);
        handles.x = getfield(data,x_var);
        data = load(handles.filename, y_var);
        handles.y = getfield(data,y_var);
        if ~isempty(handles.x) && (length(handles.x) == length(handles.y))
            if isfield(handles, 'line')
                ln_color = get(handles.line, 'color');
                ln_style = get(handles.line, 'linestyle');
                ln_marker = get(handles.line, 'marker');
            else
                ln_color = 'b';
                ln_style = '-';
                ln_marker = 'none';
            end % isfield(handles, 'line')
            handles.line = plot(handles.x, handles.y);
            xlabel('x data')
            ylabel('y data');
            % set(get(handles.my_axes,'xlabel'), 'string', 'test xlabel')
            % set(get(handles.my_axes,'ylabel'),'string', 'ylabel test')
            set(handles.line, 'color', ln_color, 'linestyle', ln_style, 'marker', ln_marker);
            guidata(hObject,handles);
        else
            dialog_gui('title','Error Message',...
                'string','Please select the variables for X and Y');
        end
    else
        dialog_gui('title','Error Message',...
            'string','Please define or load the data to plot.');
    end % get(handles.load_xy, 'value')   
end % get(handles.define_xy, 'value')

% --- Executes during object creation, after setting all properties.
function y_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y_value (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 y_value_Callback(hObject, eventdata, handles)
% hObject    handle to y_value (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 y_value as text
%        str2double(get(hObject,'String')) returns contents of y_value as a double

% plot_line_Callback(handles.plot_line, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function x_start_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x_start (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 x_start_Callback(hObject, eventdata, handles)
% hObject    handle to x_start (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 x_start as text
%        str2double(get(hObject,'String')) returns contents of x_start as a double

% --- Executes during object creation, after setting all properties.
function x_stop_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x_stop (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 x_stop_Callback(hObject, eventdata, handles)
% hObject    handle to x_stop (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 x_stop as text
%        str2double(get(hObject,'String')) returns contents of x_stop as a double

% --- Executes during object creation, after setting all properties.
function x_num_val_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x_num_val (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 x_num_val_Callback(hObject, eventdata, handles)
% hObject    handle to x_num_val (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 x_num_val as text
%        str2double(get(hObject,'String')) returns contents of x_num_val as a double


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

% Hint: get(hObject,'Value') returns toggle state of define_xy
if get(handles.define_xy, 'value')
    set(handles.load_xy, 'value', 0)
    set(handles.load_xy_objs,...
        'visible', 'off',...
        'enable', 'off')
    set(handles.define_xy_objs,...
        'visible', 'on',...
        'enable', 'on')
else
    if get(handles.load_xy, 'value')
        load_xy_Callback(handles.load_xy, [], handles)
    else
        set(handles.define_xy_objs,...
            'visible', 'on',...
            'enable', 'off')
    end
end

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

% Hint: get(hObject,'Value') returns toggle state of load_xy
if get(handles.load_xy, 'value')
    set(handles.define_xy, 'value', 0)
    set(handles.define_xy_objs,...
        'visible', 'off',...
        'enable', 'off')
    set(handles.load_xy_objs,...
        'visible', 'on',...
        'enable', 'on')
else
    if get(handles.define_xy, 'value')
        define_xy_Callback(handles.define_xy, [], handles)
    else
        set(handles.load_xy_objs,...
            'visible', 'on',...
            'enable', 'off')
    end
end

% --- Executes during object creation, after setting all properties.
function load_fname_CreateFcn(hObject, eventdata, handles)
% hObject    handle to load_fname (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_fname_Callback(hObject, eventdata, handles)
% hObject    handle to load_fname (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 load_fname as text
%        str2double(get(hObject,'String')) returns contents of load_fname as a double
fname = get(handles.load_fname, 'string');
handles.filename = fname;
guidata(hObject, handles);
if exist(fname, 'file')
    var_names = who('-file',fname);
    set([handles.x_var_list, handles.y_var_list], 'string',var_names)
else
    set([handles.x_var_list, handles.y_var_list], 'string','none')
    set([handles.x_var_list, handles.y_var_list], 'value', 1)
    dialog_gui('title','Error Message','string','Check the path & name of the MAT-file.');    
end

% --- Executes on button press in browse_fname.
function browse_fname_Callback(hObject, eventdata, handles)
% hObject    handle to browse_fname (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.mat', 'Pick a MAT-file');
set(handles.load_fname, 'string', [pathname, filename]);
load_fname_Callback(handles.load_fname,[],handles);

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

% Hint: listbox 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 x_var_list.
function x_var_list_Callback(hObject, eventdata, handles)
% hObject    handle to x_var_list (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 x_var_list contents as cell array
%        contents{get(hObject,'Value')} returns selected item from x_var_list

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

% Hint: listbox 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 y_var_list.
function y_var_list_Callback(hObject, eventdata, handles)
% hObject    handle to y_var_list (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 y_var_list contents as cell array
%        contents{get(hObject,'Value')} returns selected item from y_var_list

% --- Executes on button press in nearest_xy.
function nearest_xy_Callback(hObject, eventdata, handles)
% hObject    handle to nearest_xy (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
temp = findobj(handles.figure1,'marker', 'x');
delete(temp);

if isfield(handles, 'line')
    [x_pt,y_pt] = ginput(1);
    xdata = get(handles.line, 'xdata');
    ydata = get(handles.line, 'ydata');
    x_diff = xdata-x_pt;
    [min_val,i] = min(abs(x_diff));
    x_near = xdata(i);
    y_near = ydata(i);
    hold on
    h = plot(x_near,y_near,'rx');
    set(h, 'markersize', 8, 'linewidth', 2)
    set(handles.coord_text,...
        'string', ['x=', num2str(x_near),' y=', num2str(y_near)]);
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 



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

% --------------------------------------------------------------------
function reset_Callback(hObject, eventdata, handles)
% hObject    handle to reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
temp = findobj(handles.figure1,'marker', 'x');
delete(temp);

if isfield(handles, 'line')
    set(handles.line, 'color', 'b', 'linestyle', '-', 'marker', 'none');
else
%     msgbox('message','title','error')
    dialog_gui('title','Error Message','string','There is no line to reset.');      
end

% --------------------------------------------------------------------
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to linestyle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
temp = findobj(handles.figure1,'marker', 'x');
delete(temp);

if isfield(handles, 'line')
    delete(handles.line);
    handles = rmfield(handles,'line');
    guidata(hObject,handles);
else
    dialog_gui('title','Error Message','string','There is no line to delete.');    
end

% --------------------------------------------------------------------
function color_menu_Callback(hObject, eventdata, handles)
% hObject    handle to color_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function color_blue_Callback(hObject, eventdata, handles)
% hObject    handle to color_blue (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'color', 'b')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function color_green_Callback(hObject, eventdata, handles)
% hObject    handle to color_green (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'color', 'g')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function color_red_Callback(hObject, eventdata, handles)
% hObject    handle to color_red (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'color', 'r')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function color_magenta_Callback(hObject, eventdata, handles)
% hObject    handle to color_magenta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'color', 'm')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function color_black_Callback(hObject, eventdata, handles)
% hObject    handle to color_black (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'color', 'k')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end

% --------------------------------------------------------------------
function linestyle_menu_Callback(hObject, eventdata, handles)
% hObject    handle to linestyle_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function ls_solid_Callback(hObject, eventdata, handles)
% hObject    handle to ls_solid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'linestyle', '-');
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function ls_dotted_Callback(hObject, eventdata, handles)
% hObject    handle to ls_dotted (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'linestyle', ':');
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function ls_dashed_Callback(hObject, eventdata, handles)
% hObject    handle to ls_dashed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'linestyle', '--');
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end    

% --------------------------------------------------------------------
function marker_menu_Callback(hObject, eventdata, handles)
% hObject    handle to marker_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function marker_none_Callback(hObject, eventdata, handles)
% hObject    handle to marker_none (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'marker', 'none')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function marker_circle_Callback(hObject, eventdata, handles)
% hObject    handle to marker_circle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'marker', 'o')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function marker_diamond_Callback(hObject, eventdata, handles)
% hObject    handle to marker_diamond (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'marker', 'd')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 

% --------------------------------------------------------------------
function marker_triangle_Callback(hObject, eventdata, handles)
% hObject    handle to marker_triangle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles, 'line')
    set(handles.line, 'marker', 'v')
else
    dialog_gui('title','Error Message','string','Please plot the line first.');    
end 