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

% Last Modified by GUIDE v2.5 14-Jan-2009 17:21:26

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = PEng_1015_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 file_Callback(hObject, eventdata, handles)
% hObject    handle to file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
	
	
% --------------------------------------------------------------------
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 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)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (3)

% Användaren väljer mellan tre alternativ (variant med case)
options = {'Line', 'Dashed', 'Dotted'};
ans = listdlg('PromptString', 'Select a line style:', ...
	'SelectionMode', 'single', 'ListString', options);
if isfield(handles, 'polyline')
	switch ans
		case 1
			set(handles.polyline, 'LineStyle', '-');
		case 2
			set(handles.polyline, 'LineStyle','--');
		case 3
			set(handles.polyline, 'LineStyle',':');
	end
end

% Slut (3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (4)

% Användaren väljer mellan sju alternativ (mer "automatiserat")
options = {'Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow', 'Black'};
ans = listdlg('PromptString', 'Select a line color:', ...
	'SelectionMode', 'single', 'ListString', options);
if isfield(handles, 'polyline')
	col = {'r', 'g', 'b', 'c', 'm', 'y'};
	set(handles.polyline, 'Color', col{ans});
end

% Slut (4)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (5)

% Användaren "togglar" mellan av och på genom att välja detta menyval
gridstatus = get(hObject, 'Checked');
if strcmp(gridstatus, '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

% Slut (5)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (1)

% Dialogruta för val av fil
[filename, pathname] = uigetfile('*.txt','Load data file');
if filename ~= 0

	% Läs in filen
	filedata = load([pathname filename]);

	% Spara fildatat i handles så att andra funktioner kan nå det
	handles.filedata = filedata;
	guidata(hObject, handles);
	
	% Fråga efter önskat polynomgradtal
	str = 'Degree of interpolating polynomial';
	poldegreestr = inputdlg(str,str,1,{'2'});
	poldegree = str2num(poldegreestr{1,1});

	% Kolla att gradtalet är större än 0 och mindre än antalet datapunkter
	if poldegree > length(handles.filedata(:,1))-1
		poldegree = length(handles.filedata(:,1))-1;
		% Skapar ett varningsfönster och väntar tills användaren trycker ok
		str = ['Warning, too large! Using ' num2str(poldegree)];
		uiwait(warndlg(str,'Warning'));
	elseif poldegree < 1
		poldegree = 1;
		% Skapar ett varningsfönster och väntar tills användaren trycker ok
		str = 'Warning, too small! Using 1';
		uiwait(warndlg(str,'Warning'));
	end
	
	% Spara gradtalet i handles så att andra funktioner kan nå det
	handles.poldegree = poldegree;
	guidata(hObject, handles);
	
	% Gör minstakvadratanpassnigen och plotta
	plotdata(hObject, handles);
	
else

	% Lägg in en tom punktlista
	handles.filedata = [];
	handles.poldegree = 0;
	guidata(hObject, handles);
	
end

% --------------------------------------------------------------------
function plotdata(hObject, handles)
% EGENDEFINIERAD FUNKTION SOM PLOTTAR PUNKTERNA OCH POLYNOMET

x = handles.filedata(:,1);
y = handles.filedata(:,2);

% Plotta punkter
plot(x, y, 'r*')
hold on
axis([min(x) max(x) min(y)-2 max(y)+2])

% Plotta polynomet
p = polyfit(x, y, handles.poldegree);
polx = min(x):0.1:max(x);
poly = polyval(p, polx);
handles.polyline = plot(polx, poly, 'k--'); % Spara!
hold off

% Kolla om användaren valt grid (i Edit-menyn) på eller av
if strcmp(get(handles.grid,'Checked'),'on')
	grid on
else
	grid off
end

title(['Degree: ' num2str(handles.poldegree)]);

% Uppdatera GUI
guidata(hObject, handles)

% Slut (1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (2)

% Dialogruta där användaren får bekräfta avslut
str = 'Do you really want to exit?';
choice = questdlg(str, str, 'Exit','Cancel','Exit');
if strcmp('Exit',choice)
    close;
end

% Slut (2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
