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

% Last Modified by GUIDE v2.5 05-Nov-2009 13:16:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @demo2_OpeningFcn, ...
                   'gui_OutputFcn',  @demo2_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 demo2 is made visible.
function demo2_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 demo2 (see VARARGIN)
    set(handles.edit1,'enable','off');
  set(handles.edit2,'enable','off');
  set(handles.edit3,'enable','off');
  set(handles.pushbutton1,'enable','off');
  set(handles.pushbutton3,'enable','off');
  set(handles.mintree,'enable','off');
  set(handles.iertree,'enable','off');
  set(handles.cntrls,'enable','off');
  set(handles.cfs,'enable','off');
  global LineWidthM;
  global FontSizeM;
  global LineWidthI;
  global FontSizeI;
  global MarkerSize;
  global mintreetype;
  mintreetype=1;%MinTypeHier
  LineWidthM=0.5;
  FontSizeM=7;
  LineWidthI=2;
  FontSizeI=7;
  MarkerSize=7;
  set(handles.axes1,'Units','Normalized');
  set(handles.axes2,'Units','Normalized');
global irr_legend;
global irr_legend_handle;
  irr_legend=[];
  irr_legend_handle=[];
global min_legend;
global min_legend_handle;
  min_legend=[];
  min_legend_handle=[];

% Choose default command line output for demo2
handles.output = hObject;
handles.mintreetype=1;%MinTypeHier
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = demo2_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 during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


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


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


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global LineWidthM;
  global FontSizeM;
  global LineWidthI;
  global FontSizeI;
  global MarkerSize;
delta=str2num(get(handles.edit3,'string'));
    strt=str2num(get(handles.edit1,'string'));
    nd=str2num(get(handles.edit2,'string'));
    strt=strt-delta;
    nd=nd-delta;
        if strcmp(get(handles.marks_ier,'Checked'),'on') f21=1; else f21=0; end
        if strcmp(get(handles.colors_ier,'Checked'),'on') f22=1; else f22=0; end
        if strcmp(get(handles.alca,'Checked'),'on') hmode='alca'; else hmode='slca'; end
        axes(handles.axes1);
        cla;
        colormode=get(handles.colors_min,'Checked');
        Tmin=min_tree(strt,nd,handles.res,1,colormode,FontSizeM,LineWidthM,MarkerSize);
        if strcmp(get(handles.marks_min,'Checked'),'off')
               for i=1:length(Tmin)
                    a=get(Tmin(i),'UserData');
                    set(a.text,'visible','off');
                end
        end
           % if strcmp(get(handles.colors_min,'Checked'),'off') 
           %    for i=1:length(Tmin)
           %         set(Tmin(i),'markerfacecolor',[0 0 0]);
           %     end
           %  end
       if strcmp(get(handles.mtree,'Checked'),'off')
          for i=1:length(Tmin)
            set(Tmin(i),'visible','off');
            dat=get(Tmin(i),'UserData');
            set(dat.text,'visible','off');
            for j=1:length(dat.links,2)
                set(dat.links(j),'visible','off');
            end
            end
        end
        handles.Tmin=Tmin;
        guidata(gcbo,handles);
        axes(handles.axes2);
        cla;
        Tier=ier_tree(strt,nd,handles.res,f21,f22,hmode,FontSizeI,LineWidthI);
        if strcmp(get(handles.htree,'Checked'),'off')
           for i=1:length(Tier)
                set(Tier(i),'visible','off');
            end
            set(handles.axes2,'visible','off');
        end
        handles.Tier=Tier;
        guidata(gcbo,handles);
        set(handles.edit2,'string',num2str(nd));
        set(handles.edit1,'string',num2str(strt));

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  global LineWidthM;
  global FontSizeM;
  global LineWidthI;
  global FontSizeI;
  global MarkerSize;
delta=str2num(get(handles.edit3,'string'));
    strt=str2num(get(handles.edit1,'string'));
    nd=str2num(get(handles.edit2,'string'));
    strt=strt+delta;
    nd=nd+delta;     
        if strcmp(get(handles.marks_ier,'Checked'),'on') f21=1; else f21=0; end
        if strcmp(get(handles.colors_ier,'Checked'),'on') f22=1; else f22=0; end
        if strcmp(get(handles.alca,'Checked'),'on') hmode='alca'; else hmode='slca'; end
            axes(handles.axes1);
            cla;
            colormode=get(handles.colors_min,'Checked');
            if (strcmp(colormode,'on'))
                colormode=1;
            else
                colormode=0;
            end
            Tmin=min_tree(strt,nd,handles.res,1,colormode,FontSizeM,LineWidthM,MarkerSize);
            if strcmp(get(handles.marks_min,'Checked'),'off')
               for i=1:length(Tmin)
                    a=get(Tmin(i),'UserData');
                    set(a.text,'visible','off');
                end
            end
            if strcmp(get(handles.mtree,'Checked'),'off')
                for i=1:length(Tmin)
                    set(Tmin(i),'visible','off');
                    dat=get(Tmin(i),'UserData');
                    set(dat.text,'visible','off');
                    for j=1:size(dat.links)
                        set(dat.links(j),'visible','off');
                    end
                end
            end
            handles.Tmin=Tmin;
            guidata(gcbo,handles);
            axes(handles.axes2);
            cla;
            Tier=ier_tree(strt,nd,handles.res,f21,f22,hmode,FontSizeI,LineWidthI);
            if strcmp(get(handles.htree,'Checked'),'off')
                for i=1:length(Tier)
                    set(Tier(i),'visible','off');
                end
                set(handles.axes2,'visible','off');
            end
            handles.Tier=Tier;
            guidata(gcbo,handles);
            set(handles.edit2,'string',num2str(nd));
            set(handles.edit1,'string',num2str(strt));

% --------------------------------------------------------------------
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 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)
   global LineWidthM;
  global FontSizeM;
  global LineWidthI;
  global FontSizeI;
  global MarkerSize;
[fname,pname]=uigetfile('*.txt');
    if pname~=0
    fullname=strcat(pname,fname);
    [res,f]=my_load(fullname);
    handles.file=fullname;
   fd=fopen(fullname);%Open input file
   global col;
   global n;
   col=fscanf(fd,'%d',1);		%Read the number of columns
   n=fscanf(fd,'%d',1);		%Read the number of rows
fclose(fd);
    guidata(gcbo,handles);
     handles.res=res;
    guidata(gcbo,handles);
    [result,strt,l]=params(handles.res.dnei,handles.res.col);
        if result=='Yes'
        set(handles.pushbutton1,'enable','on');
        set(handles.pushbutton3,'enable','on');
        set(handles.edit1,'enable','on');
        set(handles.edit1,'string',strt);
        set(handles.edit2,'enable','on');
        set(handles.edit2,'string',strt+l);
        set(handles.edit3,'enable','on');
        set(handles.mintree,'enable','on');
        set(handles.iertree,'enable','on');
        set(handles.cntrls,'enable','on');
        set(handles.cfs,'enable','on');
        set(handles.marks_ier,'Checked','on');
        set(handles.colors_ier,'Checked','on');
        set(handles.htree,'Checked','on');
        set(handles.marks_min,'Checked','on');
        set(handles.marks_min,'Checked','on');
        set(handles.mtree,'Checked','on');
        set(handles.axes1,'position',[0.05 .32 .43 .6]);
        set(handles.axes2,'position',[.52 .32 .43 .6]);
        set(handles.axes2,'visible','on');
        set(handles.axes1,'visible','on');
        axes(handles.axes1);
        cla;
        colormode=1;
        set(handles.colors_min,'Checked','on');
        Tmin=min_tree(strt,strt+l,handles.res,1,colormode,FontSizeM,LineWidthM,MarkerSize);
        handles.Tmin=Tmin;
        guidata(gcbo,handles);
        axes(handles.axes2);
        cla;
        if strcmp(get(handles.slca,'Checked'),'on')
            mode='slca';
        else
            mode='alca';
        end
        %[x y]=avultradist(strt,strt+l,handles.res,1,1,mode,FontSizeI,LineWidthI);
        %plot(x,y);
        %pause(60);
        Tier=ier_tree(strt,strt+l,handles.res,1,1,mode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end
        set(gcf,'PaperPositionMode','auto');
    end
% --------------------------------------------------------------------
function mintree_Callback(hObject, eventdata, handles)
% hObject    handle to mintree (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function marks_min_Callback(hObject, eventdata, handles)
% hObject    handle to marks_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(get(gcbo,'Checked'),'on')
        set(gcbo,'Checked','off');
        for i=1:length(handles.Tmin)
            a=get(handles.Tmin(i),'UserData');
            set(a.text,'visible','off');
        end
    else
        set(gcbo,'Checked','on');
        for i=1:length(handles.Tmin)
            a=get(handles.Tmin(i),'UserData');
            set(a.text,'visible','on');
        end       
    end    

% --------------------------------------------------------------------
function colors_min_Callback(hObject, eventdata, handles)
% hObject    handle to colors_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
if strcmp(get(gcbo,'Checked'),'on')
        set(gcbo,'Checked','off');
        %for i=1:length(handles.Tmin)
            %set(handles.Tmin(i),'markerfacecolor',[0 0 0]);
            %set(handles.Tmin(i),'markerfacecolor','none');
        %    a=get(handles.Tmin(i),'UserData');
        %    set(handles.Tmin(i),'markerfacecolor',a.color_bw);
        %    set(handles.Tmin(i),'MarkerEdgeColor',a.color_bw1);
        %    size=get(handles.Tmin(i),'MarkerSize');
        %    set(handles.Tmin(i),'LineWidth',size/3);
        %end
    else
        set(gcbo,'Checked','on');
        %for i=1:length(handles.Tmin)
        %    a=get(handles.Tmin(i),'UserData');
        %    set(handles.Tmin(i),'markerfacecolor',a.color);
        %    set(handles.Tmin(i),'LineWidth',0.5);
        %end
    end    
step=get(handles.edit3,'String');
set(handles.edit3,'String',0);
pushbutton1_Callback(hObject, eventdata, handles)
set(handles.edit3,'String',step);

% --------------------------------------------------------------------
function mtree_Callback(hObject, eventdata, handles)
% hObject    handle to mtree (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(get(gcbo,'Checked'),'on')
        set(gcbo,'Checked','off');
        %axes(handles.axes1);
        set(handles.colors_min,'enable','off');
        set(handles.marks_min,'enable','off');
        for i=1:length(handles.Tmin)
            set(handles.Tmin(i),'visible','off');
            dat=get(handles.Tmin(i),'UserData');
            set(dat.text,'visible','off');
            for j=1:size(dat.links,2)
                set(dat.links(j),'visible','off');
            end
        end
        set(handles.axes1,'visible','off');
        pos=get(handles.axes2,'position');
        pos(1)=.05;
        pos(3)=.9;
        set(handles.axes2,'position',pos);
        set(handles.text5,'visible','off');
    else
        set(gcbo,'Checked','on');
        set(handles.colors_min,'enable','on');
        set(handles.marks_min,'enable','on');
        for i=1:length(handles.Tmin)
            set(handles.Tmin(i),'visible','on');
            dat=get(handles.Tmin(i),'UserData');
            set(dat.text,'visible','on');
            for j=1:size(dat.links,2)
                set(dat.links(j),'visible','on');
            end
        end
        set(handles.axes1,'visible','on');
        pos=get(handles.axes2,'position');
        pos(1)=.52;
        pos(3)=.43;
        set(handles.axes2,'position',pos);
        if strcmp(get(handles.visible,'Checked'),'on')
            set(handles.text5,'visible','on');
        end
    end

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


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

  global LineWidthI;
  global FontSizeI;
if strcmp(get(handles.slca,'Checked'),'on') hmode='slca'; else hmode='alca'; end
if strcmp(get(gcbo,'Checked'),'on')
        set(gcbo,'Checked','off');
        axes(handles.axes2);
        cla;
        beg=str2num(get(handles.edit1,'String'));
        ed=str2num(get(handles.edit2,'String'));
        if strcmp(get(handles.colors_ier,'Checked'),'on')
        Tier=ier_tree(beg,ed,handles.res,0,1,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        else
        Tier=ier_tree(beg,ed,handles.res,0,0,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end
    else
        set(gcbo,'Checked','on');
        axes(handles.axes2);
        cla;
        beg=str2num(get(handles.edit1,'String'));
        ed=str2num(get(handles.edit2,'String'));
        if strcmp(get(handles.colors_ier,'Checked'),'on')
        Tier=ier_tree(beg,ed,handles.res,1,1,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        else
        Tier=ier_tree(beg,ed,handles.res,1,0,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end
    end

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

  global LineWidthI;
  global FontSizeI;
if strcmp(get(handles.slca,'Checked'),'on') hmode='slca'; else hmode='alca'; end
        %set(gcbo,'Checked','on');
        axes(handles.axes2);
        cla;
        beg=str2num(get(handles.edit1,'String'));
        ed=str2num(get(handles.edit2,'String'));
         if strcmp(get(handles.marks_ier,'Checked'),'on')
        Tier=ier_tree(beg,ed,handles.res,1,1,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        else
        Tier=ier_tree(beg,ed,handles.res,0,1,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end
        

% --------------------------------------------------------------------
function htree_Callback(hObject, eventdata, handles)
% hObject    handle to htree (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(get(gcbo,'Checked'),'on')
        set(gcbo,'Checked','off');
        set(handles.axes2,'visible','off');
        set(handles.colors_ier,'enable','off');
        set(handles.marks_ier,'enable','off');
        set(handles.mode,'enable','off');
        for i=1:length(handles.Tier)
            set(handles.Tier(i),'visible','off');
        end
        pos=get(handles.axes1,'position');
        pos(3)=.9;
        set(handles.axes1,'position',pos);
        set(handles.text6,'visible','off');
    else
        set(gcbo,'Checked','on');
        set(handles.axes2,'visible','on');
        set(handles.colors_ier,'enable','on');
        set(handles.marks_ier,'enable','on');
        set(handles.mode,'enable','on');
        for i=1:length(handles.Tier)
            set(handles.Tier(i),'visible','on');
        end
        pos=get(handles.axes1,'position');
        pos(3)=.43;
        set(handles.axes1,'position',pos);
        if strcmp(get(handles.visible,'Checked'),'on')
            set(handles.text6,'visible','on');
        end
    end

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


% --------------------------------------------------------------------
function visible_Callback(hObject, eventdata, handles)
% hObject    handle to visible (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(get(gcbo,'Checked'),'on')
        set(gcbo,'Checked','off');
            set(handles.edit1,'visible','off');
            set(handles.edit2,'visible','off');
            set(handles.edit3,'visible','off');
            set(handles.pushbutton1,'visible','off');
            set(handles.pushbutton3,'visible','off');
            set(handles.text4,'visible','off');
            set(handles.text2,'visible','off');
            set(handles.text3,'visible','off');
            set(handles.text5,'visible','off');
            set(handles.text6,'visible','off');
            pos1=get(handles.axes1,'Position');
            pos2=get(handles.axes2,'Position');
            pos1(2)=.1;
            pos2(2)=.1;
            pos1(4)=.85;
            pos2(4)=.85;
            set(handles.axes1,'Position',pos1);
            set(handles.axes2,'Position',pos2);
        else
            set(gcbo,'Checked','on');
            set(handles.edit1,'visible','on');
            set(handles.edit2,'visible','on');
            set(handles.edit3,'visible','on');
            set(handles.pushbutton1,'visible','on');
            set(handles.pushbutton3,'visible','on');
            set(handles.text4,'visible','on');
            set(handles.text2,'visible','on');
            set(handles.text3,'visible','on');
            set(handles.text5,'visible','on');
            set(handles.text6,'visible','on');
            pos1=get(handles.axes1,'Position');
            pos2=get(handles.axes2,'Position');
            pos1(2)=.32;
            pos2(2)=.32;
            pos1(4)=.6;
            pos2(4)=.6;
            set(handles.axes1,'Position',pos1);
            set(handles.axes2,'Position',pos2);
        end


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


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

  global LineWidthI;
  global FontSizeI;
beg=str2num(get(handles.edit1,'String'));
ed=str2num(get(handles.edit2,'String'));
if strcmp(get(handles.alca,'Checked'),'off')
   set(gcbo,'Checked','on');
   set(handles.slca,'Checked','off');
   mode='alca';
   axes(handles.axes2);
   cla;
   Tier=ier_tree(beg,ed,handles.res,1,1,mode,FontSizeI,LineWidthI);
   handles.Tier=Tier;
   guidata(gcbo,handles);
end

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

  global LineWidthI;
  global FontSizeI;
beg=str2num(get(handles.edit1,'String'));
ed=str2num(get(handles.edit2,'String'));
if strcmp(get(handles.slca,'Checked'),'off')
   set(gcbo,'Checked','on');
   set(handles.alca,'Checked','off');
   mode='slca';
   axes(handles.axes2);
   cla;
   Tier=ier_tree(beg,ed,handles.res,1,1,mode,FontSizeI,LineWidthI);
   handles.Tier=Tier;
   guidata(gcbo,handles);
end


% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 mp=get(gcbo,'CurrentPoint');
 x2=mp(2,1);
 y2=mp(2,2);
 l=findobj(handles.Tmin,'Marker','<');
 if length(l)>0
     l=l(1);
 end
 
 x1=get(l,'XData');
 y1=get(l,'YData');
 dx=x2-x1;
 dy=y2-y1;
 set(l,'XData',mp(2,1));
 set(l,'YData',mp(2,2));
% set(l,'Marker','o');
 a=get(l,'UserData');
 pos=get(a.text,'Position');
 if (strcmp(get(handles.colors_min,'Checked'),'off'))
     %setshape(l,a.color_bw(1));
     set(l,'Marker',a.symvol);
 else
     set(l,'Marker','o');
 end
 set(a.text,'Position',[pos(1)+dx,pos(2)+dy,pos(3)]);
 xpos=get(a.border,'XData');
 ypos=get(a.border,'YData');
% set(a.border,'Position',[xpos+dx,ypos+dy,pos(3)]);
 set(a.border,'XData',xpos+dx);
 set(a.border,'YData',ypos+dy);

 for i=1:size(a.links,2)
     x=get(a.links(i),'XData');
     y=get(a.links(i),'YData');
     if x(1)==x1 & y(1)==y1
         x(1)=x(1)+dx;
         y(1)=y(1)+dy;
     else
         x(2)=x(2)+dx;
         y(2)=y(2)+dy;
     end
     set(a.links(i),'XData',x);
     set(a.links(i),'YData',y);
 end


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


% --------------------------------------------------------------------
function c_cluster_Callback(hObject, eventdata, handles)
% hObject    handle to c_cluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global coefs;
figure;
plot(coefs.clust);
title('Clustering coefficient');
figure;
plot(coefs.clust2);
title('Clustering coefficient of a single node');
% --------------------------------------------------------------------
function c_diam_Callback(hObject, eventdata, handles)
% hObject    handle to c_diam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global coefs;
figure;
plot(coefs.diam);
title('Diameter');
% --------------------------------------------------------------------
function c_avgdist_Callback(hObject, eventdata, handles)
% hObject    handle to c_avgdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global coefs;
figure;
plot(coefs.asp);
title('Average shortest path');
% --------------------------------------------------------------------
function c_maxdegree_Callback(hObject, eventdata, handles)
% hObject    handle to c_maxdegree (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global coefs;
figure;
plot(coefs.maxdegree);
title('Max degree');

% --------------------------------------------------------------------
function c_all_Callback(hObject, eventdata, handles)
% hObject    handle to c_all (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    res='';
fd=fopen(handles.file);%Open input file
col=fscanf(fd,'%d',1);		%Read the number of columns
n=fscanf(fd,'%d',1);		%Read the number of rows
    fd=fopen(handles.file);%Open input file
    col=fscanf(fd,'%d',1);		%Read the number of columns
    n=fscanf(fd,'%d',1);		%Read the number of rows

    [res,ln1,bg1,nd1,step1,thr1]=c_params2;
    democoef(bg1,nd1,ln1,step1,handles.file,thr1,0);


% --------------------------------------------------------------------
function minedit_Callback(hObject, eventdata, handles)
% hObject    handle to minedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    global LineWidthM;
    global FontSizeM;
    global MarkerSize;
    [result,FontSizeM,LineWidthM,MarkerSize]=editplot2(FontSizeM,LineWidthM,MarkerSize);
    if strcmp(result,'OK')
       Tmin=handles.Tmin;
       for i=1:length(Tmin);
           set(Tmin(i),'MarkerSize',MarkerSize);
           a=[];
           a=get(Tmin(i),'UserData');
           txt=a.text;
           set(txt,'FontSize',FontSizeM);
           links=a.links;
           for j=1:size(links,2)
               set(links(j),'LineWidth',LineWidthM);
           end
       end
   end
    


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    global LineWidthI;
    global FontSizeI;
    [result,FontSizeI,LineWidthI]=editplot(FontSizeI,LineWidthI);
    if strcmp(result,'OK')
       Tier=handles.Tier;
       for i=1:length(Tier);
           a=[];
           a=get(Tier(i),'UserData');
           if a=='line'
               set(Tier(i),'LineWidth',LineWidthI);
           else
                set(Tier(i),'FontSize',FontSizeI);
           end
       end
   end


% --- Executes when figure1 window is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sss=get(handles.figure1,'PaperSize');
pos2=get(handles.slider1,'Position');
pos2(3)=sss(2);%wisota
%set(handles.slider1,'Position',pos2);

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


% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA
   global LineWidthM;
  global FontSizeM;
  global LineWidthI;
  global FontSizeI;
  global MarkerSize;
[fname,pname]=uigetfile('*.txt');
    if pname~=0
    fullname=strcat(pname,fname);
    [res,f]=my_load_matr(fullname);
    handles.file=fullname;
   fd=fopen(fullname);%Open input file
   global col;
   global n;
   col=fscanf(fd,'%d',1);		%Read the number of columns
   n=fscanf(fd,'%d',1);		%Read the number of rows
fclose(fd);
    guidata(gcbo,handles);
     handles.res=res;
    guidata(gcbo,handles);
    [result,strt,l]=params(handles.res.dnei,handles.res.col);
        if result=='Yes'
        set(handles.pushbutton1,'enable','on');
        set(handles.pushbutton3,'enable','on');
        set(handles.edit1,'enable','on');
        set(handles.edit1,'string',strt);
        set(handles.edit2,'enable','on');
        set(handles.edit2,'string',strt+l);
        set(handles.edit3,'enable','on');
        set(handles.mintree,'enable','on');
        set(handles.iertree,'enable','on');
        set(handles.cntrls,'enable','on');
        set(handles.cfs,'enable','on');
        set(handles.marks_ier,'Checked','on');
        set(handles.colors_ier,'Checked','on');
        set(handles.htree,'Checked','on');
        set(handles.marks_min,'Checked','on');
        set(handles.marks_min,'Checked','on');
        set(handles.mtree,'Checked','on');
        set(handles.axes1,'position',[0.05 .32 .43 .6]);
        set(handles.axes2,'position',[.52 .32 .43 .6]);
        set(handles.axes2,'visible','on');
        set(handles.axes1,'visible','on');
        axes(handles.axes1);
        cla;
        Tmin=min_tree(strt,strt+l,handles.res,1,1,FontSizeM,LineWidthM,MarkerSize);
        handles.Tmin=Tmin;
        guidata(gcbo,handles);
        axes(handles.axes2);
        cla;
        if strcmp(get(handles.slca,'Checked'),'on')
            mode='slca';
        else
            mode='alca';
        end
        %[x y]=avultradist(strt,strt+l,handles.res,1,1,mode,FontSizeI,LineWidthI);
        %plot(x,y);
        %pause(60);
        Tier=ier_tree(strt,strt+l,handles.res,1,1,mode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end
        set(gcf,'PaperPositionMode','auto');
    end


% --- Executes on mouse press over axes background.
function axes2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%% resize!!!


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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
mp=get(hObject,'Value');
pos=get(handles.axes2,'Position');
pos(2)= mp;%(2)-(coords(2)-y2);
pos(4)=1-mp-0.1;%sss(4)+(coords(2)-y2);
 set(handles.axes2,'Position',pos);
pos=get(handles.axes1,'Position');
pos(2)= mp;%(2)-(coords(2)-y2);
pos(4)=1-mp-0.1;%sss(4)+(coords(2)-y2);
 set(handles.axes1,'Position',pos);


% --------------------------------------------------------------------
function HideSlider_Callback(hObject, eventdata, handles)
% hObject    handle to HideSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
vis=get(handles.slider1,'Visible');
if (strcmp(vis,'on')==1)
    set(handles.slider1,'Visible','off');
else
    set(handles.slider1,'Visible','on');
end


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


% --------------------------------------------------------------------
function MinTypeHier_Callback(hObject, eventdata, handles)
% hObject    handle to MinTypeHier (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global mintreetype;
mintreetype=1;%MinTypeHier
step=get(handles.edit3,'String');
set(handles.edit3,'String',0);
pushbutton1_Callback(hObject, eventdata, handles)
set(handles.edit3,'String',step);
% --------------------------------------------------------------------
function MinTypeBranches_Callback(hObject, eventdata, handles)
% hObject    handle to MinTypeBranches (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global mintreetype
mintreetype=2;%MinTypeBranches
step=get(handles.edit3,'String');
set(handles.edit3,'String',0);
pushbutton1_Callback(hObject, eventdata, handles)
set(handles.edit3,'String',step);


% --------------------------------------------------------------------
function Mintree_legend_Callback(hObject, eventdata, handles)
% hObject    handle to Mintree_legend (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global min_legend;
if isempty(min_legend)
    min_legend=1;
    set(handles.Mintree_legend,'Checked','On');
else
    min_legend=[];
    set(handles.Mintree_legend,'Checked','Off');
end
step=get(handles.edit3,'String');
set(handles.edit3,'String',0);
pushbutton1_Callback(hObject, eventdata, handles)
set(handles.edit3,'String',step);


% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global irr_legend;
global irr_legend_handle;
if (strcmp(get(handles.Untitled_4,'Checked'),'on'))
    set(handles.Untitled_4,'Checked','off');
    if ~isempty(irr_legend_handle)
        legend(irr_legend_handle,'hide');
        set(irr_legend_handle,'Visible','Off');
    end
    irr_legend=0;
else
    set(handles.Untitled_4,'Checked','on');
    irr_legend=1;
    if ~isempty(irr_legend_handle)
        legend(irr_legend_handle,'show');
        set(irr_legend_handle,'Visible','On');
    end
   
  global LineWidthI;
  global FontSizeI;

        if strcmp(get(handles.slca,'Checked'),'on') hmode='slca'; else hmode='alca'; end
        axes(handles.axes2);
        cla;
        beg=str2num(get(handles.edit1,'String'));
        ed=str2num(get(handles.edit2,'String'));
        axes(handles.axes2);
        cla;

        if strcmp(get(handles.marks_ier,'Checked'),'on')
        Tier=ier_tree(beg,ed,handles.res,1,0,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        else
        Tier=ier_tree(beg,ed,handles.res,0,0,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end
    end

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


% --------------------------------------------------------------------
function Iertree_bw_Callback(hObject, eventdata, handles)
% hObject    handle to Iertree_bw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  global LineWidthI;
  global FontSizeI;
if strcmp(get(handles.slca,'Checked'),'on') hmode='slca'; else hmode='alca'; end

        %set(gcbo,'Checked','on');
        
        axes(handles.axes2);
        cla;
        beg=str2num(get(handles.edit1,'String'));
        ed=str2num(get(handles.edit2,'String'));
        axes(handles.axes2);
        cla;

        if strcmp(get(handles.marks_ier,'Checked'),'on')
        Tier=ier_tree(beg,ed,handles.res,1,2,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        else
        Tier=ier_tree(beg,ed,handles.res,0,2,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end



% --------------------------------------------------------------------
function Iertree_gray_Callback(hObject, eventdata, handles)
% hObject    handle to Iertree_gray (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  global LineWidthI;
  global FontSizeI;
if strcmp(get(handles.slca,'Checked'),'on') hmode='slca'; else hmode='alca'; end

        %set(gcbo,'Checked','on');
        
        beg=str2num(get(handles.edit1,'String'));
        ed=str2num(get(handles.edit2,'String'));
        axes(handles.axes2);
        cla;

        if strcmp(get(handles.marks_ier,'Checked'),'on')
        Tier=ier_tree(beg,ed,handles.res,1,0,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        else
        Tier=ier_tree(beg,ed,handles.res,0,0,hmode,FontSizeI,LineWidthI);
        handles.Tier=Tier;
        guidata(gcbo,handles);
        end



% --------------------------------------------------------------------
function ShuffleMatrix_Callback(hObject, eventdata, handles)
% hObject    handle to ShuffleMatrix (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global peremesh;
if isempty(peremesh)
    peremesh=0;
end
if peremesh==0
    peremesh=1;
    set(hObject,'Checked','On');
else
    peremesh=0;
    set(hObject,'Checked','Off');
end

