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

% Last Modified by GUIDE v2.5 26-Sep-2006 11:04:46

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

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

% Update handles structure
guidata(hObject, handles);

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


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


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(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)


% --- 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)
[fname,pname]=uigetfile('*.txt');
if pname~=0
filename=strcat(pname,fname);
fd=fopen(filename,'r');
n=fscanf(fd,'%d',1);%kolichestwo firm
col=fscanf(fd,'%d',1);%dlina ryada
filedt=strrep(filename,'.txt','.dt');
a=[];				%a  is a array of input data
for j=1:col		%Cycle for all columns
    b=fscanf(fd,'%e',n);	%Read a row of numbers
    a=[a;transpose(b)];		%Add a row to data array
end
a=transpose(a);%Transpose input data
str=[];				%Str is a list of the names for the columns
str1=cell(n,1);
for i=1:n			%Cycle for columns
    b=fscanf(fd,'%s',1);	%Read the name
    str(i).name=b;		%adds the name to str
    str1(i)={b};
end

set(handles.listbox1,'string',str1);
colors=[];
for i=1:n			%Cycle for columns
    b=fscanf(fd,'%d',1);	%Read the name
    str(i).color=b;		%adds the name to str
end
set(handles.edit1,'string',1);
set(handles.edit2,'string',col);
fclose(fd);			%Close input file
input.data=a;
input.n=n;
input.col=col;
input.str=str;
guidata(gcbo,handles);
handles.input=input;
guidata(gcbo,handles);
end

% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1
x=get(handles.listbox1,'value');
dat=handles.input.data;
beg=str2double(get(handles.edit1,'string'));
fin=str2double(get(handles.edit2,'string'));
set(handles.editName,'string',handles.input.str(x).name);
set(handles.editColor,'string',handles.input.str(x).color);
y=dat(x,beg:fin);
hold off;
plot(y,'b');
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton2.
function pushbutton2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=get(handles.listbox1,'value');
dat=handles.input.data;
beg=str2double(get(handles.edit1,'string'));
fin=str2double(get(handles.edit2,'string'));
y=dat(x,beg:fin);
hold off;
plot(y,'b');


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton3.
function pushbutton3_ButtonDownFcn(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)


% --- 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)
x=get(handles.listbox1,'value');
dat=handles.input.data;
beg=str2double(get(handles.edit1,'string'));
fin=str2double(get(handles.edit2,'string'));
y=dat(x,beg:fin);
y1=[y,0];
y2=[0,y];
ret=y2-y1;
hold off;
plot(ret,'b');


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton4.
function pushbutton4_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fname,pname]=uiputfile('*.txt');
if pname~=0
filename=strcat(pname,fname);
fd=fopen(filename,'w');
guidata(gcbo,handles);
input=handles.input;
guidata(gcbo,handles);
n=input.n;
col=input.col;
str=input.str;
a=input.data;

fprintf(fd,'%d ',n);%kolichestwo firm
fprintf(fd,'%d\n',col);%dlina ryada
filedt=strrep(filename,'.txt','.dt');
for j=1:col		%Cycle for all columns
   fprintf(fd,'%e ',a(:,j));	%Read a row of numbers
   fprintf(fd,'\n');
end
for i=1:n			%Cycle for columns
    fprintf(fd,'%s\n',str(i).name);	%Read the name
end
for i=1:n			%Cycle for columns
    fprintf(fd,'%d\n',str(i).color);	%Read the name
end
fclose(fd);
end

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over listbox1.
function listbox1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=get(handles.listbox1,'value');
dat=handles.input.data;
beg=str2double(get(handles.edit1,'string'));
fin=str2double(get(handles.edit2,'string'));
y=dat(x,beg:fin);
y1=[y,0];
y2=[0,y];
ret=y2-y1;
hold off;
plot(ret,'b');
end


% --------------------------------------------------------------------
function dAddMenu_Callback(hObject, eventdata, handles)
% hObject    handle to dAddMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
input=handles.input;
str=input.str;
n=input.n;
col=input.col;
x=get(handles.listbox1,'value');
dat=handles.input.data;
[fname,pname]=uigetfile('*.txt');
if pname~=0
    filename=strcat(pname,fname);
    y=dlmread(filename,' ');
    if (size(dat,2)==size(y,2))
        begdat=dat(1:x,:);
        enddat=dat(x+1:n,:);
        dat=[begdat;y;enddat];
        begdat=str(1:x);
        enddat=str(x+1:n);
        insert.name='NoiName';
        insert.color=10;
        str=[begdat,insert,enddat];
        str1=cells(n+1,1);
        for i=1:n+1
            str1(i,1)={str(i).name};
        end
       set(handles.listbox1,'string',str1);
       input.data=dat;
       input.n=n+1;
       input.col=col;
       input.str=str;
       guidata(gcbo,handles);
       handles.input=input;
       guidata(gcbo,handles);
   end
end
end



% --------------------------------------------------------------------
function dDelMenu_Callback(hObject, eventdata, handles)
% hObject    handle to dDelMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=get(handles.listbox1,'value');
guidata(gcbo,handles);
input=handles.input;
a=input.data;
n=input.n;
str=input.str;
if x<n
    a=[a(1:x-1,:);a(x+1:n,:)];
    str=[str(1:x-1),str(x+1:n)];
else
    a=a(1:x-1,:);
    str=str(1:x-1);
end
input.data=a;
input.n=n-1;
input.str=str;
handles.input=input;
str1=cells(n-1,1);
for i=1:n-1
    str1(i,1)={str(i).name};
end
if (x==n)||(x==n-1) 
    set(handles.listbox1,'value',x-1);
end
set(handles.listbox1,'string',str1);
guidata(gcbo,handles);


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


% --------------------------------------------------------------------
function d_SaveMenu_Callback(hObject, eventdata, handles)
% hObject    handle to d_SaveMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=get(handles.listbox1,'value');
dat=handles.input.data;
y=dat(x,:);
[fname,pname]=uiputfile('*.txt');
if pname~=0
filename=strcat(pname,fname);
dlmwrite(filename,y,' ');
%fd=fopen(filename,'r');
end




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


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


% --------------------------------------------------------------------
function Bases_load_Callback(hObject, eventdata, handles)
% hObject    handle to Bases_load (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fname,pname]=uigetfile('*.txt');
if pname~=0
filename=strcat(pname,fname);
fd=fopen(filename,'r');
n=fscanf(fd,'%d',1);%kolichestwo firm
col=fscanf(fd,'%d',1);%dlina ryada
filedt=strrep(filename,'.txt','.dt');
a=[];				%a  is a array of input data
for j=1:col		%Cycle for all columns
    b=fscanf(fd,'%e',n);	%Read a row of numbers
    a=[a;transpose(b)];		%Add a row to data array
end
a=transpose(a);%Transpose input data
str=[];				%Str is a list of the names for the columns
str1=cell(n,1);
for i=1:n			%Cycle for columns
    b=fscanf(fd,'%s',1);	%Read the name
    str(i).name=b;		%adds the name to str
    str1(i)={b};
end

set(handles.listbox1,'string',str1);
colors=[];
for i=1:n			%Cycle for columns
    b=fscanf(fd,'%d',1);	%Read the name
    str(i).color=b;		%adds the name to str
end
set(handles.edit1,'string',1);
set(handles.edit2,'string',col);
fclose(fd);			%Close input file
input.data=a;
input.n=n;
input.col=col;
input.str=str;
guidata(gcbo,handles);
handles.input=input;
guidata(gcbo,handles);
end

% --------------------------------------------------------------------
function Bases_save_Callback(hObject, eventdata, handles)
% hObject    handle to Bases_save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fname,pname]=uiputfile('*.txt');
if pname~=0
filename=strcat(pname,fname);
fd=fopen(filename,'w');
guidata(gcbo,handles);
input=handles.input;
guidata(gcbo,handles);
n=input.n;
col=input.col;
str=input.str;
a=input.data;
fprintf(fd,'%d ',n);%kolichestwo firm
fprintf(fd,'%d\n',col);%dlina ryada
filedt=strrep(filename,'.txt','.dt');
for j=1:col		%Cycle for all columns
   fprintf(fd,'%e ',a(:,j));	%Read a row of numbers
   fprintf(fd,'\n');
end
for i=1:n			%Cycle for columns
    fprintf(fd,'%s\n',str(i).name);	%Read the name
end
for i=1:n			%Cycle for columns
    fprintf(fd,'%d\n',str(i).color);	%Read the name
end
fclose(fd);
end


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


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

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

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

% --------------------------------------------------------------------
function Load_Matrix_Callback(hObject, eventdata, handles)
% hObject    handle to Load_Matrix (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fname,pname]=uigetfile('*.txt');
if pname~=0
filename=strcat(pname,fname);
filenm=strrep(filename,'.txt','.nm');
dat=dlmread(filename,' ');

n=size(dat,1);
col=size(dat,2);
str=[];
str2=cells(1,n);
fd=fopen(filenm,'r');
for i=1:n
    if (~fd)
        str(i).name=strcat('noname',num2str(i));
    else
        str(i).name=fscanf(fd,'%s',1);
    end
    str(i).color=0;
    str2(i)={str(i).name};
end
set(handles.listbox1,'string',str2);
set(handles.edit1,'string',1);
set(handles.edit2,'string',col);
input.data=dat;
input.n=n;
input.col=col;
input.str=str;
guidata(gcbo,handles);
handles.input=input;
guidata(gcbo,handles);

end

% --------------------------------------------------------------------
function mnLoad_names_Callback(hObject, eventdata, handles)
% hObject    handle to mnLoad_names (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fname,pname]=uigetfile('*.nm');
if pname~=0
filename=strcat(pname,fname);
input=handles.input;
n=input.n;
dat=input.data;
col=input.col;
str=[];
str2=cells(1,n);
fd=fopen(filename,'r');
for i=1:n
    str(i).name=fscanf(fd,'%s',1);
    ch=fgetc(fd);
    str1=fgetl(fd);
    if (strlen(str1)<2)
        str1=str(i).name;
    end
    str(i).color=0;
    str2(i)={str1};
end
set(handles.listbox1,'string',str2);
input.data=dat;
input.n=n;
input.col=col;
input.str=str;
guidata(gcbo,handles);
handles.input=input;
guidata(gcbo,handles);
end

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


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


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


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


% --- Executes during object creation, after setting all properties.
function editName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editName (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 editName_Callback(hObject, eventdata, handles)
% hObject    handle to editName (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 editName as text
%        str2double(get(hObject,'String')) returns contents of editName as a double
newname=get(handles.editName,'string');
x=get(handles.listbox1,'value');
input=handles.input;
input.str(x).name=newname;
str1=get(handles.listbox1,'string');
str1(x)={newname};
set(handles.listbox1,'string',str1);
guidata(gcbo,handles);
handles.input=input;
guidata(gcbo,handles);


% --- Executes during object creation, after setting all properties.
function editColor_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editColor (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 editColor_Callback(hObject, eventdata, handles)
% hObject    handle to editColor (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 editColor as text
%        str2double(get(hObject,'String')) returns contents of editColor as a double
newcolor=str2double(get(handles.editColor,'string'));
x=get(handles.listbox1,'value');
input=handles.input;
input.str(x).color=newcolor;
guidata(gcbo,handles);
handles.input=input;
guidata(gcbo,handles);

