function varargout = uigetlimit(varargin)
% $Id: uigetnoise.m 6 2004-12-13 12:28:39Z maik $
% $Id: uigetlimit.m 6 2005-9-28 robert $
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @uigetlimit_OpeningFcn, ...
'gui_OutputFcn', @uigetlimit_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 uigetlimit is made visible.
function uigetlimit_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 uigetlimit (see VARARGIN)
% Parameter1 = Matrix(nx2): time,Signal_Graph1
% Parameter2 = Signal_Graph2 (if not given, takes Signal_Graph1)
% Parameter3 = Window-titel
% Parameter4 = Text/instruction
% Parameter5 = parameter to obtain from signal2 (default: mean)
% Parameter6 = size of window to be displayed in gui (default: 20s)
handles = guihandles(gcf);
handles.winsize=20; %default windowsize in seconds
handles.parameter='@mean';%default function handle
% check whether we got any daty
if (nargin<3+3)
return;
end
%ist input a matrix?
if(~isnumeric(varargin{1}) | size(varargin{1},1)<=1 | size(varargin{1},2)<=1)
return;
end
%valid matrix: save in handles structure
handles.data = varargin{1};
handles.sr=round(1000/nanmean(diff(handles.data(1:100,1))));
handles.data(:,1)=handles.data(:,1)/1000; %display seconds instead of milliseconds
%set title etc. with remaining Input-parameters
if isstr(varargin{2})
handles.data=[handles.data handles.data(:,2)];
set(handles.figure1,'Name',varargin{2});
set(handles.text,'String',varargin{3});
handles.parameter=varargin{4};
else
handles.data=[handles.data varargin{2}];
set(handles.figure1,'Name',varargin{3});
set(handles.text,'String',varargin{4});
end
%make a function handle of given parameter
if nargin>4+3
parameter=varargin{5};
if parameter(1)=='@' %anonymous function given, e.g. '@(x)max(x)*0.7'
eval(['parameter=' parameter ';']);
else %name of a function given
parameter=str2func(varargin{5});
end
if isa(parameter,'function_handle');
handles.parameter=parameter;
end
end
if nargin>5+3
handles.winsize=varargin{6};
end
% Update handles structure
guidata(hObject, handles);
% Center GUI
%
dlgRes=get(handles.figure1,'position');
monRes=get(0,'ScreenSize');
dlgRes(1)=(monRes(3)-dlgRes(3))/2; %x-achse
dlgRes(2)=(monRes(4)/2)-(dlgRes(4)/2); %x-achse
set(handles.figure1,'position',dlgRes);
%%-------------------------------------
% hide slider?
if(handles.winsize>=handles.data(end,1))
set(handles.slider,'Visible','off');
else
minstep=(handles.winsize/handles.data(end,1))*0.3;
maxstep=(handles.winsize/handles.data(end,1))*0.7;
set(handles.slider,'SliderStep',[minstep maxstep]);
set(handles.slider,'Value',(handles.winsize/handles.data(end,1)));
end
%*******************************************
% Plot Signal 1 in Graph 1
axes(handles.Graph1);
plot(handles.data(:,1),handles.data(:,2));
axeslims=[handles.data(1,1) (handles.data(handles.winsize*handles.sr,1))];
set(handles.Graph1,'Xlim',axeslims);
set(handles.Graph1,'NextPlot','add');%hold; %for drawing rectangles...
linkaxes([handles.Graph1 handles.Graph2],'x');
set(gca,'SelectionHighlight','off')%no selection handles visible
MyButtonDownFcn='uigetlimit(''Graph1_ButtonDownFcn'', gcbo,[],guidata(gcbo))'; %calls function in this file...
set(handles.Graph1,'ButtonDownFcn',MyButtonDownFcn);
%*******************************************
% Plot Signal 2 in Graph 2
axes(handles.Graph2);
plot(handles.data(:,1),handles.data(:,3),'r');
axeslims=[handles.data(1,1) (handles.data(handles.winsize*handles.sr,1))];
set(handles.Graph2,'Xlim',axeslims);
%*******************************************
% compute starting value for output value:
currentwindow=find(handles.data(:,1)>axeslims(1) & handles.data(:,1)<axeslims(2));
handles.output=handles.parameter(handles.data(currentwindow,3));
handles.limitline=line([handles.data(1,1) handles.data(end,1)],[handles.output handles.output],'Color','b');
handles.ynew=[handles.output handles.output];
set(handles.limitline,'Tag','limitline');
set(handles.limitline,'Selected','Off');
lineupdate=['set(gco,''Selected'',''on'');setptr(gcf,''closedhand'');']; %ButtonDown for line function...
set(handles.limitline,'ButtonDownFcn',lineupdate);
set(handles.editLimit,'String',num2str(handles.output));
guidata(gcf,handles) %tried it with hObject, got an error
% UIWAIT makes uigetlimit wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = uigetlimit_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
if(isfield(handles,'output'))
varargout{1} = handles.output;
close();
else
varargout{1} = -1;
end
% --- Executes on button press in bCancel.
function bCancel_Callback(hObject, eventdata, handles)
% hObject handle to bCancel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.output=-1;
guidata(hObject, handles);
uiresume(handles.figure1);
% --- Executes on button press in bNext.
function bNext_Callback(hObject, eventdata, handles)
% hObject handle to bNext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% check value
limit=str2double(get(handles.editLimit,'String'));
if isnan(limit)
return;
end
% save result
handles.output=[limit 1];
guidata(hObject, handles);
%close dialogue
uiresume(handles.figure1);
function editLimit_Callback(hObject, eventdata, handles)
% hObject handle to editLimit (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 editLimit as text
% str2double(get(hObject,'String')) returns contents of editLimit as a double
newlimit = str2double(get(hObject,'string'));
if isnan(newlimit)
errordlg('invalid value for limit','Error','modal')
return
end
axes(handles.Graph2);
set(findobj('Tag','limitline'),'YData',[newlimit newlimit]);
% --- Executes during object creation, after setting all properties.
function editLimit_CreateFcn(hObject, eventdata, handles)
% hObject handle to editLimit (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
% --- Executes on slider movement.
function slider_Callback(hObject, eventdata, handles)
% hObject handle to slider (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
slidervalue=get(handles.slider,'Value');
set(handles.Graph1,'YLimMode','auto')
set(handles.Graph2,'YLimMode','auto')
axeslims=[(slidervalue*handles.data(end,1)-0.5*handles.winsize) slidervalue*handles.data(end,1)+0.5*handles.winsize];
set([handles.Graph1 handles.Graph2],'XLim',axeslims)
% --- Executes during object creation, after setting all properties.
function slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider (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 mouse press over axes background.
function Graph1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to Graph1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Graph1,'Selected','On');
startpoint= get(handles.Graph1,'CurrentPoint'); % button down detected
handles.startpoint=startpoint(1,1); % only x-Coordinate
handles.selection=[];
guidata(gcf,handles)
% --- Executes on mouse press over axes background.
function Graph2_ButtonDownFcn(hObject, eventdata, handles,varargin)
% hObject handle to Graph2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% unused: uses ButtonDownFcn of limitline and WindowButtonMotionFcn (see below) instead
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(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)
handles=guidata(gcf); %I had problems without it or with guidata(hObject)
if isfield(handles,'ynew') %sometimes problems
ynew=handles.ynew;
else
ynew=get(findobj('Tag','limitline'),'YLim');
end
if strcmp(get(findobj('Tag','limitline'),'Selected'),'on') %if limitline in Graph2 is selected
ytemp=get(handles.Graph2,'CurrentPoint');
if ~isempty(ytemp)
ynew=ytemp(1:2,2);
set(findobj('Tag','limitline'),'YData',ynew);
handles.ynew=ynew;
set(handles.editLimit,'String',num2str(handles.ynew(1)));
guidata(hObject,handles);
end
elseif strcmp(get(handles.Graph1,'Selected'),'on') %if user wants to drag rectangle in Graph1
startpoint=handles.startpoint;
endpoint=get(handles.Graph1,'Currentpoint');
endpoint=endpoint(1,1);
currenty=get(handles.Graph1,'YLim');
if ~isempty(handles.selection)
delete(handles.selection)
end
handles.selection=fill([startpoint endpoint endpoint startpoint],...
[currenty(1) currenty(1) currenty(2) currenty(2)],[1 0 0],...
'EraseMode','xor','LineStyle',':');
guidata(hObject,handles);
else
return
end
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(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)
if strcmp(get(findobj('Tag','limitline'),'Selected'),'on') %if limitline in Graph2 is selected
handles.output=handles.ynew(1); %take y value of limitline as new limit
set(findobj('Tag','limitline'),'Selected','off');
setptr(gcf,'arrow');
guidata(hObject,handles)
elseif strcmp(get(handles.Graph1,'Selected'),'on')
set(handles.Graph1,'Selected','off');
endpoint=get(handles.Graph1,'Currentpoint');
endpoint=endpoint(1,1:2);
xlimits=sort([handles.startpoint(1) endpoint(1)]);
delete(handles.selection); %clear selection rectangle
valid=(handles.data(:,1)>=xlimits(1) & handles.data(:,1)<=xlimits(2));
handles.output=handles.parameter(handles.data(valid,3));
else
return
end
set(handles.editLimit,'String',num2str(handles.output));
set(findobj('Tag','limitline'),'YData',[handles.output handles.output]);