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

% Last Modified by GUIDE v2.5 17-Feb-2010 15:54:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guiDCmotor_OpeningFcn, ...
                   'gui_OutputFcn',  @guiDCmotor_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 guiDCmotor is made visible.
function guiDCmotor_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 guiDCmotor (see VARARGIN)
handles.output = hObject;
p=inputParser;
p.addParamValue('pokyn',@(varargin)(eval('disp([mfilename ''/pokyn:'']),disp(varargin)')));
p.addParamValue('hParent',[]);
p.addParamValue('nazev','Ovládání MultiUnit');
p.addParamValue('nastaveni',[]);
p.addParamValue('data',[]);
p.parse(varargin{:})
handles.pokyn=p.Results.pokyn;
handles.nastaveni=p.Results.nastaveni;
handles.data=p.Results.data;
set(handles.guiNazev,'String',p.Results.nazev);
%keyboard
% if ~isfield(handles.nastaveni,'frekvence')
% handles.nastaveni.frekvence=215;
% end
% set(handles.editfrek,'String',handles.nastaveni.frekvence);
% set(handles.STfrek,'String',[num2str(round(16e6/2/handles.nastaveni.frekvence/100)/10) ' kHz'])
%keyboard
%nacte do gui prikazy:
if isfield(handles.nastaveni,'atprikazy')
    delkapr=length(handles.nastaveni.atprikazy);
    if ~isfield(handles.nastaveni.atprikazy,'typ'),for npr=1:delkapr,handles.nastaveni.atprikazy(npr).typ='-';end,end
for npr=1:delkapr, prikazy(npr,:)={handles.nastaveni.atprikazy(npr).nazev,char(handles.nastaveni.atprikazy(npr).typ),handles.nastaveni.atprikazy(npr).napoveda};end
set(handles.uitableprikazy,'Data',prikazy)
end
%nacte do gui promene:
if isfield(handles.nastaveni,'mereni')
delkamer=length(handles.nastaveni.mereni);
if ~isfield(handles.nastaveni.mereni,'meritko'),for npr=1:delkamer,handles.nastaveni.mereni(npr).meritko=1;end,end
if ~isfield(handles.nastaveni.mereni,'merit'),for npr=1:delkamer,handles.nastaveni.mereni(npr).merit=logical(false);end,end
if ~isfield(handles.nastaveni.mereni,'barva'),for npr=1:delkamer,handles.nastaveni.mereni(npr).barva='Blue';end,end
%keyboard
for npr=1:delkamer, mereni(npr,:)={handles.nastaveni.mereni(npr).nazev,char(handles.nastaveni.mereni(npr).typ),handles.nastaveni.mereni(npr).merit,handles.nastaveni.mereni(npr).meritko,handles.nastaveni.mereni(npr).bytu,char(handles.nastaveni.mereni(npr).barva),handles.nastaveni.mereni(npr).napoveda};end
set(handles.uitablepromene,'Data',mereni);
%keyboard
    if isempty({mereni(:,1)'})
        keyboard
    end
if isempty(mereni(:,1)),keyboard,end
    set(handles.uitablescope,'ColumnFormat',{mereni(:,1)'})
%set(handles.uitablescope,'Data',mereni(:,1))
end

if isfield(handles.nastaveni,'atfiltry')
delkafiltru=length(handles.nastaveni.atfiltry);
for npr=1:delkafiltru, filtry(npr,:)={[handles.nastaveni.atfiltry(npr).nazev ' ' handles.nastaveni.atfiltry(npr).napoveda]};end
set(handles.popupmenufiltry,'String',filtry);
end

%keyboard
%init
%set(handles.uitablescope,'Data',{[]});
%set(handles.uitablescope,'Data',{'pwm'})
plot(tan([(1:1000)/100]+rand(1,1000)*10))

handles.historieprikazu={''};
handles.seznamfiltry=[];
handles.hOvladac=[];
handles.nastaveni.osaYhorni=inf;
handles.nastaveni.osaYdolni=-inf;
handles.graf=handles.axes1;
% Choose default command line output for guiDCmotor
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes guiDCmotor wait for user response (see UIRESUME)
% uiwait(handles.DCmotor);
%       prikaz - fce pro ovladani gui z venku
function prikaz(nazev,h,hodn)
%keyboard
if exist('nazev','var')
if exist(nazev)
    fce=str2func(nazev);
    if exist('hodn')
       fce(h,hodn); 
    else
        fce(h);
    end
else
    disp([mfilename '/Neznamy příkaz: ' nazev]);
end,end

% --- Outputs from this function are returned to the command line.
function varargout = guiDCmotor_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;
varargout{2} = @prikaz;


% --- Executes on button press in keyboard.

% hObject    handle to keyboard (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 keyboard.
function keyboard_Callback(hObject, eventdata, handles)
% hObject    handle to keyboard (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
keyboard

function updatehistorie(hObject, eventdata, handles)
%keyboard
delka=size(handles.historieprikazu);
if ~delka
   set(handles.listboxhistorie,'String',{}) 
else
for n=1:delka(1)
    if isempty(char(handles.historieprikazu(n,1)))
    d(n)=handles.historieprikazu(n,2);
    else
    d(n)={[char(handles.historieprikazu(n,1)) '  -  ' char(handles.historieprikazu(n,2))]};
    end
end
set(handles.listboxhistorie,'String',d)
end
if isempty(get(handles.listboxhistorie,'Value'))
set(handles.listboxhistorie,'Value',1)
end
guidata(hObject,handles);

function updatefiltru(hObject, eventdata, handles)
%keyboard

d=get(handles.popupmenufiltry,'String');
set(handles.listboxfiltry,'String',d(handles.seznamfiltry));
guidata(hObject,handles);

% --- Executes on button press in pushbuttonatprikaz.
function pushbuttonatprikaz_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonatprikaz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.pokyn('atpokyn',handles.atprikazodesli)
%keyboard
handles.historieprikazu(2:end+1,:)=handles.historieprikazu;
handles.historieprikazu(1,1)={handles.atprikazinfo};
handles.historieprikazu(1,2)={handles.atprikazodesli};
updatehistorie(hObject, eventdata, handles)
%keyboard
guidata(hObject,handles);
%keyboard

function editatprikaz_Callback(hObject, eventdata, handles)
% hObject    handle to editatprikaz (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 editatprikaz as text
%        str2double(get(hObject,'String')) returns contents of editatprikaz as a double

d=get(handles.uitableprikazy,'Data');
dn=d(:,1);dt=d(:,2);
i=find(strcmp(d,char(get(handles.textatprikaz,'String'))));
c=str2num(get(handles.editatprikaz,'String'));
if strcmp(dt(i),'char')
    handles.atprikazodesli=[char(get(handles.textatprikaz,'String')) ' ' char(get(handles.editatprikaz,'String')) ';'];
    handles.atprikazinfo='';
    set(handles.editatprikazcely,'String',handles.atprikazodesli)
elseif strcmp(dt(i),'-')||isempty(c)
    handles.atprikazodesli=[char(get(handles.textatprikaz,'String')) ';'];
    handles.atprikazinfo='';
    set(handles.editatprikazcely,'String',handles.atprikazodesli)

else
    %keyboard
    cp=eval([char(dt(i)) '(' 'c' ')']);
    handles.atprikazodesli=[char(get(handles.textatprikaz,'String')) ' ' typecast(cp,'uint8') ';'];
    handles.atprikazinfo=[char(get(handles.textatprikaz,'String')) ' ' num2str(double(cp)) ';'];
    set(handles.editatprikazcely,'String',[handles.atprikazinfo '   -   ' handles.atprikazodesli])
end
guidata(hObject,handles)
%keyboard

% --- Executes during object creation, after setting all properties.
function editatprikaz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editatprikaz (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editatprikazcely_Callback(hObject, eventdata, handles)
% hObject    handle to editatprikazcely (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 editatprikazcely as text
%        str2double(get(hObject,'String')) returns contents of editatprikazcely as a double


% --- Executes during object creation, after setting all properties.
function editatprikazcely_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editatprikazcely (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonpovolit.
function pushbuttonpovolit_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonpovolit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.pokyn('atpokyn','povolit;')

% --- Executes on button press in pushbuttonzakazat.
function pushbuttonzakazat_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonzakazat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.pokyn('atpokyn','zakazat;')

% --- Executes on button press in pushbuttonscope.
function pushbuttonscope_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonscope (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
nazev=char(get(handles.uitablescope,'Data'));
handles.pokyn('atpokyn',['scope ' nazev ';']);
pause(.1);
handles.pokyn('atprijemscope',nazev);

% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (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 listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listboxhistorie.
function listboxhistorie_Callback(hObject, eventdata, handles)
% hObject    handle to listboxhistorie (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 listboxhistorie contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxhistorie
%keyboard

% --- Executes during object creation, after setting all properties.
function listboxhistorie_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxhistorie (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function v=prevod(text,cislo,format)
keyboard


% --- Executes when selected cell(s) is changed in uitableprikazy.
function uitableprikazy_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to uitableprikazy (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)
d=get(handles.uitableprikazy,'Data');
if ~isempty(eventdata.Indices)
if eventdata.Indices(2)==1
set(handles.textatprikaz,'String',d(eventdata.Indices(1),1))
guidata(hObject,handles)
end
end
editatprikaz_Callback(hObject, eventdata, handles);


% --- Executes on key press with focus on listboxhistorie and none of its controls.
function listboxhistorie_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to listboxhistorie (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
if strcmp(eventdata.Key,'delete')
    in=get(handles.listboxhistorie,'Value');
%    keyboard
    ds={};
    if ~isempty(handles.historieprikazu(1:in-1,:))
    ds=handles.historieprikazu(1:in-1,:);
    end
    if ~isempty(handles.historieprikazu(in+1:end,:))
        if isempty(ds)
        ds=handles.historieprikazu(in+1:end,:);
        else
        %keyboard
            ds=[ds;handles.historieprikazu(in+1:end,:)];
        end
    end
    handles.historieprikazu=ds;
    delka=size(handles.historieprikazu);
    if (in>delka(1))&&(delka(1)~=0)
set(handles.listboxhistorie,'Value',delka(1));
end
            guidata(hObject,handles);
updatehistorie(hObject, eventdata, handles)
end


if strcmp(eventdata.Key,'return')
 
    in=get(handles.listboxhistorie,'Value');
    if ~isempty(in)
        handles.pokyn('atpokyn',char(handles.historieprikazu(in,2)));
    end
end



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over editatprikaz.
function editatprikaz_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to editatprikaz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isempty(handles.hOvladac)
handles.hOvladac=ovladac('hParent',hObject,... 
 'pokyn',@prikaz,'nazevhodnoty','parametr','nazev','Parametr','limity',[0 1000 0 -1000]);
guidata(hObject,handles);
end

% --- Executes on button press in checkboxprizmene.
function checkboxprizmene_Callback(hObject, eventdata, handles)
% hObject    handle to checkboxprizmene (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkboxprizmene

function parametr(hObject,f)
handles=guidata(hObject);
set(handles.editatprikaz,'String',round(f));
guidata(hObject,handles);
%disp ahoj
editatprikaz_Callback(hObject, [], handles);
handles=guidata(hObject);
if get(handles.checkboxprizmene,'Value')
pushbuttonatprikaz_Callback(hObject, [], handles);
end

function uloz(a,h)
handles=guidata(h);
handles.hOvladac=[];
guidata(h,handles);

function scope(hObject,d)
handles=guidata(hObject);
dp=get(handles.uitablepromene,'Data');
dh=typecast(d.data,char(dp(find(strcmp(dp(:,1),d.nazev)),2)));
set(handles.textscope,'String',num2str(dh))
set(handles.textscope,'TooltipString',num2str(dh))
%keyboard

function ATdata(hObject,d)
handles=guidata(hObject);
%keyboard
if isempty(d)
    keyboard
end

pdata=get(handles.uitablepromene,'Data');%nacist nastaveni promenych
ind=find([pdata{:,3}]==1);%ziska indexy aktualne merenych promenych
if (size(d.y,2)==size(ind,2)) %musi souhlasit dimenze doslych dat a zvolenych promenych
    for in=1:length(ind)
        plot(handles.graf,d.x,d.y(:,in)*pdata{ind(in),4},'color',pdata{ind(in),6});
        hold(handles.graf,'on')
    end
    hold(handles.graf,'off')
else
   disp([mfilename '\ATdata nesouhlasi dimenze prijatych dat a zvolenych!']) 
keyboard
end
%plot(handles.axes1,d.x,d.y)
axis(handles.axes1,[-inf inf handles.nastaveni.osaYdolni handles.nastaveni.osaYhorni]);
%keyboard

% --- Executes when entered data in editable cell(s) in uitablepromene.
function uitablepromene_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitablepromene (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)

if eventdata.Indices(2)==3 %pokud se zmeni seznam prijimanych hodnot
  d=get(handles.uitablepromene,'Data');
    in=find([d{:,3}]==1);
    if isempty(in)
        handles.pokyn('vybermereni',[]);
    else
        handles.pokyn('vybermereni',d(in,[1 2 5]));
    end
    %   
end


%ochrana proti nezadani cisla nebo ciselneho vztahu:
if eventdata.Indices(2)==4 %pokud se zmeni seznam prijimanych hodnot
    d=get(handles.uitablepromene,'Data');
    cislo=str2num(eventdata.EditData);
    if isnumeric(cislo)
       d{eventdata.Indices(1),4}=cislo;
    else
        d{eventdata.Indices(1),4}=eventdata.PreviousData;
    end

set(handles.uitablepromene,'Data',d);   
guidata(hObject,handles);
 %    keyboard
end

% --- Executes on button press in checkboxmer.
function checkboxmer_Callback(hObject, eventdata, handles)
% hObject    handle to checkboxmer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkboxmer
handles.pokyn('prubeznyprenos',get(hObject,'Value'))



function editosaYhorni_Callback(hObject, eventdata, handles)
% hObject    handle to editosaYhorni (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 editosaYhorni as text
%        str2double(get(hObject,'String')) returns contents of editosaYhorni as a double
c=str2num(get(hObject,'String'));
if isempty(c)
   set(hObject,'String',handles.nastaveni.osaYhorni)
else
    handles.nastaveni.osaYhorni=c;
end
guidata(hObject,handles)
axis(handles.axes1,[-inf inf handles.nastaveni.osaYdolni handles.nastaveni.osaYhorni]);



% --- Executes during object creation, after setting all properties.
function editosaYhorni_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editosaYhorni (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editosaYdolni_Callback(hObject, eventdata, handles)
% hObject    handle to editosaYdolni (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 editosaYdolni as text
%        str2double(get(hObject,'String')) returns contents of editosaYdolni as a double
c=str2num(get(hObject,'String'));
if isempty(c)
   set(hObject,'String',handles.nastaveni.osaYdolni)
else
    handles.nastaveni.osaYdolni=c;
end
guidata(hObject,handles)
axis(handles.axes1,[-inf inf handles.nastaveni.osaYdolni handles.nastaveni.osaYhorni]);


% --- Executes during object creation, after setting all properties.
function editosaYdolni_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editosaYdolni (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object deletion, before destroying properties.
function DCmotor_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to DCmotor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isfield(handles,'nastaveni')
    
if isfield(handles.nastaveni,'atprikazy')
handles.nastaveni=rmfield(handles.nastaveni,'atprikazy');end
if isfield(handles.nastaveni,'mereni')
handles.nastaveni=rmfield(handles.nastaveni,'mereni');end

d=get(handles.uitablepromene,'Data');
%keyboard
delka=size(d);
for npr=1:delka(1)
        handles.nastaveni.mereni(npr).nazev=d{npr,1};
        handles.nastaveni.mereni(npr).typ=d{npr,2};
%        handles.nastaveni.mereni(npr).merit=d(npr,3);
        handles.nastaveni.mereni(npr).meritko=d{npr,4};
        handles.nastaveni.mereni(npr).bytu=d{npr,5};
        handles.nastaveni.mereni(npr).barva=d{npr,6};
        handles.nastaveni.mereni(npr).napoveda=d{npr,7};
end

d=get(handles.uitableprikazy,'Data');
for npr=1:length(d) 
        handles.nastaveni.atprikazy(npr).nazev=d{npr,1};
        handles.nastaveni.atprikazy(npr).typ=d{npr,2};
        handles.nastaveni.atprikazy(npr).napoveda=d{npr,3};
        end


handles.pokyn('uloz',handles.nastaveni);
handles.pokyn('konec');
end

% --- Executes on button press in pushbuttonulozitmereni.
function pushbuttonulozitmereni_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonulozitmereni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.pokyn('ulozitmereni',get(handles.editmereninazev,'String'));



function editmereninazev_Callback(hObject, eventdata, handles)
% hObject    handle to editmereninazev (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 editmereninazev as text
%        str2double(get(hObject,'String')) returns contents of editmereninazev as a double


% --- Executes during object creation, after setting all properties.
function editmereninazev_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editmereninazev (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on uitableprikazy and none of its controls.
function uitableprikazy_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to uitableprikazy (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
%keyboard
if strcmp(eventdata.Key,'space')
pushbuttonatprikaz_Callback(hObject, eventdata, handles)
end


% --- Executes on selection change in listboxfiltry.
function listboxfiltry_Callback(hObject, eventdata, handles)
% hObject    handle to listboxfiltry (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 listboxfiltry contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxfiltry


% --- Executes during object creation, after setting all properties.
function listboxfiltry_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxfiltry (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenufiltry.
function popupmenufiltry_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenufiltry (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 popupmenufiltry contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenufiltry
handles.seznamfiltry(end+1)=get(hObject,'Value');
updatefiltru(hObject, eventdata, handles);
%keyboard

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

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on listboxfiltry and none of its controls.
function listboxfiltry_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to listboxfiltry (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

if strcmp(eventdata.Key,'delete')
    in=get(handles.listboxfiltry,'Value');
    %keyboard
    ds=[];
    if ~isempty(handles.seznamfiltry(1:in-1))
    ds=handles.seznamfiltry(1:in-1);
    end
    if ~isempty(handles.seznamfiltry(in+1:end))
        if isempty(ds)
        ds=handles.seznamfiltry(in+1:end);
        else
        %keyboard
            ds=[ds handles.seznamfiltry(in+1:end)];
        end
    end
    handles.seznamfiltry=ds;
    delka=size(handles.seznamfiltry);
    if (in>delka(2)&&(delka(2)~=0))
set(handles.listboxfiltry,'Value',delka(2));
end
            guidata(hObject,handles);
updatefiltru(hObject, eventdata, handles)
end
% --- Executes on button press in pushbuttonfiltry.
function pushbuttonfiltry_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonfiltry (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%keyboard

d=handles.seznamfiltry;
delka=length(d);
text='filtrmsk';
if ~isempty(d)
    text=[text ' ' handles.nastaveni.atfiltry(d(1)).nazev];
    if (delka>1)
        for n=2:delka
            text=[text ',' handles.nastaveni.atfiltry(d(n)).nazev];
        end
    end
end
text=[text ';'];
handles.pokyn('atpokyn',text);
