function varargout = teneleven(varargin)
% AnvŠnder ett grafiskt grŠnssnitt med komponenterna:
% ritomrŒde: axes1
% Arkiv-meny med:
% Arkiv>…ppna: Open
% Arkiv>Avsluta: Quit
% Redigera-meny med:
% Redigera>FŠrg>Ršd: Red
% Redigera>FŠrg>Gršn: Green
% Redigera>FŠrg>BlŒ: Blue
% Redigera>FŠrg>Cyan: Cyan
% Redigera>FŠrg>Magenta: Magenta
% Redigera>FŠrg>Gul: Yellow
% Redigera>FŠrg>Svart: Black
% Redigera>Linjetyp>Ingen: LineNone
% Redigera>Linjetyp>Heldragen: LineSolid
% Redigera>Linjetyp>Prickad: LineDotted
% Redigera Markšrtyp>Ingen: MarkerNone
% Redigera Markšrtyp>o: MarkerCircle
% Redigera Markšrtyp>.: MarkerDot
% Redigera Markšrtyp>x: MarkerCross
% Redigera>Grad>0: Degree0
% Redigera>Grad>1: Degree1
% Redigera>Grad>2: Degree2
% Redigera>Grad>3: Degree3
% Redigera>Grad>4: Degree4
% Redigera>Grad>5: Degree5
%% Läser data från en fil och gör minsta kvadrat
%% anpassning till dessa data. Infilen har ett x-y-par per rad
% NINEELEVENA Application M-file for nineeleven.fig
%%    FIG = TENELEVEN launch nineeleven GUI.
%%    TENELEVEN('callback_name', ...) invoke the named callback.
% Last Modified by GUIDE v2.5 06-Feb-2004 18:39:46

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);
    % Sätt skönsvärdet på grad till 1, rät linje
    % markera datapunkterna med ringar
    % rita polynomet med heldragen svart linje
    handles.degree = 1;
    handles.marker = 'o';
    handles.line = '-';
    handles.linecol = 'b';
    handles.figure1 = fig;
    guidata(fig, handles);
    if nargout > 0
        varargout{1} = fig;
    end
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
    try
        [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
    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 Open_Callback(hObject, eventdata, handles)
%% Fråga efter infil, läs koordinatdata och spar i figure-objektet
[infile,infilepath] = uigetfile(...
    '/Volumes/sromFickskiva/SR30Nump/MATLABEX/mymatlab/*.dat','Välj indatafil');
if infile~=0
    ifr = fopen([infilepath infile]);
end
z= fscanf(ifr,'%f',[2 Inf]);
handles.x = z(1,:);
handles.y = z(2,:);
guidata(handles.figure1,handles);
draw(handles);

function Quit_Callback(hObject, eventdata, handles)
%% Avbryt programmet
s = questdlg('Vill du verkligen avsluta programmet?','','Avsluta','Avbryt','Avbryt');
if strcmp(s,'Avsluta')
    close(gcbf);
end

function draw(handles)
% Rita data och polynom med senast valda data
x = handles.x;
y = handles.y;
% Radera tidigare diagram i ritområdet
cla(handles.axes1);
% Rita datapunkterna med referens till ritområdet som första parameter
plot(handles.axes1,x,y,handles.marker);
c = polyfit(x,y,handles.degree);
p = polyval(c,x);
% Rita polynomet
plot(handles.axes1,x,p,[handles.line handles.linecol]);

% --------------------------------------------------------------------
function Red_Callback(hObject, eventdata, handles)
% hObject    handle to LineRed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='r';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Green_Callback(hObject, eventdata, handles)
% hObject    handle to LineGreen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='g';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Blue_Callback(hObject, eventdata, handles)
% hObject    handle to LineBlue (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='b';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Cyan_Callback(hObject, eventdata, handles)
% hObject    handle to LineCyan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='c';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Magenta_Callback(hObject, eventdata, handles)
% hObject    handle to LineMagenta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='m';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Yellow_Callback(hObject, eventdata, handles)
% hObject    handle to LineYellow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='y';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Black_Callback(hObject, eventdata, handles)
% hObject    handle to LineBlack (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.linecol='k';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Degree0_Callback(hObject, eventdata, handles)
% hObject    handle to Degree0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.degree=0;
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Degree1_Callback(hObject, eventdata, handles)
% hObject    handle to Degree1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.degree=1;
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Degree2_Callback(hObject, eventdata, handles)
% hObject    handle to Degree2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.degree=2;
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Degree3_Callback(hObject, eventdata, handles)
% hObject    handle to Degree3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.degree=3;
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Degree4_Callback(hObject, eventdata, handles)
% hObject    handle to Degree4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.degree=4;
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function Degree5_Callback(hObject, eventdata, handles)
% hObject    handle to Degree5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.degree=5;
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function MarkerNone_Callback(hObject, eventdata, handles)
% hObject    handle to MarkerNone (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.marker='';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function MarkerCircle_Callback(hObject, eventdata, handles)
% hObject    handle to MarkerCircle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.marker='o';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function MarkerDot_Callback(hObject, eventdata, handles)
% hObject    handle to MarkerDot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.marker='.';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function MarkerCross_Callback(hObject, eventdata, handles)
% hObject    handle to MarkerCross (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.marker='x';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function LineNone_Callback(hObject, eventdata, handles)
% hObject    handle to LineNone (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.line='';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function LineSolid_Callback(hObject, eventdata, handles)
% hObject    handle to LineSolid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.line='-';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------
function LineDotted_Callback(hObject, eventdata, handles)
% hObject    handle to LineDotted (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.line=':';
guidata(hObject,handles);
draw(handles);

% --------------------------------------------------------------------


