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

% editmenu the above text to modify the response to help ex_10_15

% Last Modified by GUIDE v2.5 20-Feb-2008 15:54:32

% Begin initialization code - DO NOT EDITMENU
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ex_10_15_OpeningFcn, ...
                   'gui_OutputFcn',  @ex_10_15_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 EDITMENU


% --- Executes just before ex_10_15 is made visible.
function ex_10_15_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 ex_10_15 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = ex_10_15_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;

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


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

% öppna dialogruta
[filename, pathname] = uigetfile('*.txt','Load Data');

if ne(filename,0)
    % öppna filen
    fildata = load(filename);

    % spara datan till handle-struktur
    handles.fildata = fildata;
    guidata(hObject, handles);

    % fråga efter gradtal
    str = 'Degree of interpolating polynomial';
    degree = inputdlg(str,str,1,{'2'});
    d = str2num(degree{1,1});

    if d > length(handles.fildata(:,1))-1
        d = length(handles.fildata(:,1))-1;
        str = ['Warning, to large! Using ' num2str(d)];
        uiwait(warndlg(str,'Warning','non-modal'));
    elseif d < 1
        d = 1;
        str = ['Warning, must be 1 or larger'];
        uiwait(warndlg(str,'Warning','non-modal'));
    end
    handles.pdegree = d;
    guidata(hObject, handles);

    % Ställ in gradtalet
    set(handles.degreeslider,'Max',length(handles.fildata(:,1))-1);
    set(handles.degreeslider,'Min',1);
    set(handles.degreeslider,'Value',d);

    plotdata(hObject, handles);
end

% --------------------------------------------------------------------
function plotdata(hObject, handles)
    x = handles.fildata(:,1);
    y = handles.fildata(:,2);

    plot(x,y,'k*'); hold on;
    axis([min(x) max(x) min(y)-2 max(y)+2]);
    p = polyfit(x,y,handles.pdegree);
    x1 = min(x):0.1:max(x);
    y1 = polyval(p,x1);
    handles.lin = plot(x1,y1,'k--'); hold off

    if strcmp(get(handles.grid,'Checked'),'on')
        grid on
    else
        grid off
    end

    guidata(hObject, handles);
    set(handles.degreetext,'String',...
        ['Degree: ' num2str(handles.pdegree)]);

% --------------------------------------------------------------------
function exit_Callback(hObject, eventdata, handles)
% hObject    handle to exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = 'Do you really want to exit?';
button = questdlg(str,str,'Exit','Cancel','Exit');
if strcmp('Exit',button)
    close;
end


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


% --------------------------------------------------------------------
function linestyle_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)
options = {'Line','Dashed','Dotted'};
ansv = listdlg('ListString',options,'SelectionMode','single', ...
                'ListSize',[160 100]);
if isfield(handles,'lin')
    switch ansv
        case 1
            set(handles.lin,'LineStyle','-');
        case 2
            set(handles.lin,'LineStyle','--');
        case 3
            set(handles.lin,'LineStyle',':');
    end
end

% --------------------------------------------------------------------
function linecolour_Callback(hObject, eventdata, handles)
% hObject    handle to linecolour (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
options = {'Black','Blue','Red'};
ansv = listdlg('ListString',options,'SelectionMode','single', ...
                'ListSize',[160 100]);
if isfield(handles,'lin')
    switch ansv
        case 1
            set(handles.lin,'Color','k');
        case 2
            set(handles.lin,'Color','b');
        case 3
            set(handles.lin,'Color','r');
    end
end

% --------------------------------------------------------------------
function grid_Callback(hObject, eventdata, handles)
% hObject    handle to grid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
gr = get(hObject,'Checked');
if strcmp(gr,'off')
    set(gca,'XGrid','on');
    set(gca,'YGrid','on');
    set(hObject,'Checked','on');
else
    set(gca,'XGrid','off');
    set(gca,'YGrid','off');
    set(hObject,'Checked','off');
end

% --- Executes on slider movement.
function degreeslider_Callback(hObject, eventdata, handles)
% hObject    handle to degreeslider (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

% först kolla om vi laddat in en fil
if isfield(handles, 'fildata')
    d1 = get(hObject, 'Value');
    d1 = round(d1);
    handles.pdegree = d1;
    set(hObject, 'Value', d1);
    plotdata(hObject, handles);
end


% --- Executes during object creation, after setting all properties.
function degreeslider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to degreeslider (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