function new_params = optostim_calibrate(varargin) % optostim_CALIBRATE Application M-file for optostim_calibrate.fig % FIG = optostim_CALIBRATE launch optostim_calibrate GUI. % optostim_CALIBRATE('callback_name', ...) invoke the named callback. % Last Modified by GUIDE v2.0 09-Apr-2003 15:26:06 %if nargin == 0 | nargin == 2 | isnumeric(varargin(1)) % LAUNCH GUI if nargin == 2 % LAUNCH GUI data = varargin{1}; params = varargin{2}; fig = openfig(mfilename,'reuse'); % Use system color scheme for figure: set(fig,'Color',get(0,'defaultUicontrolBackgroundColor')); % Generate a structure of handles to pass to callbacks, and store it. handles = guihandles(fig); set(handles.editX_0, 'String', num2str(params(1))); set(handles.editK_x, 'String', num2str(params(2))); set(handles.editY_0, 'String', num2str(params(3))); set(handles.editK_y, 'String', num2str(params(4))); handles.new_params = params; handles.data = data; guidata(fig, handles) refresh(handles.axes1, data, params); % Wait for callbacks to run and window to be dismissed: uiwait(fig); % UIWAIT might have returned because the window was deleted using % the close box - in that case, return original params, and % don't bother deleting the window! if ~ishandle(fig) new_params = params ; else % otherwise, we got here because the user pushed "Apply" button. % retrieve the latest copy of the 'handles' struct, and return the answer. % Also, we need to delete the window. handles = guidata(fig); new_params = handles.new_params; new_params_child = new_params delete(fig); end %if nargout > 0 % varargout{1} = fig; %end elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK try if (nargout) [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard else feval(varargin{:}); % FEVAL switchyard end catch disp(lasterr); end end %| ABOUT CALLBACKS: %| GUIDE automatically appends subfunction prototypes to this file, and %| sets objects' callback properties to call them through the FEVAL %| switchyard above. This comment describes that mechanism. %| %| Each callback subfunction declaration has the following form: %| (H, EVENTDATA, HANDLES, VARARGIN) %| %| The subfunction name is composed using the object's Tag and the %| callback type separated by '_', e.g. 'slider2_Callback', %| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'. %| %| H is the callback object's handle (obtained using GCBO). %| %| EVENTDATA is empty, but reserved for future use. %| %| HANDLES is a structure containing handles of components in GUI using %| tags as fieldnames, e.g. handles.figure1, handles.slider2. This %| structure is created at GUI startup using GUIHANDLES and stored in %| the figure's application data using GUIDATA. A copy of the structure %| is passed to each callback. You can store additional information in %| this structure at GUI startup, and you can change the structure %| during callbacks. Call guidata(h, handles) after changing your %| copy to replace the stored original so that subsequent callbacks see %| the updates. Type "help guihandles" and "help guidata" for more %| information. %| %| VARARGIN contains any extra arguments you have passed to the %| callback. Specify the extra arguments by editing the callback %| property in the inspector. By default, GUIDE sets the property to: %| ('', gcbo, [], guidata(gcbo)) %| Add any extra arguments after the last argument, before the final %| closing parenthesis. function refresh(this_axes, data, params) axes(this_axes) plot(data(:,1), data(:,2), 'ro') xlimits = get(gca, 'XLim'); x = xlimits(1):(xlimits(2)-xlimits(1))/100:xlimits(2); y = curve(x, params); hold on plot(x,y) hold off function yy = curve(xx, params) yy = params(3) + params(4)*tan((xx-params(1))/params(2)) ; function params_final = autofit(data, start_params) % default fitting: options = optimset('Display','off','TolX',0.1); params_final = fminsearch('optostim_cal_error', start_params, options, data); % refresh: x = data(:,1); y = data(:,2); plot(x,y,'or'); hold on x_ = x(1):(x(size(x,1))-x(1))/100:x(size(x,1)); plot(x_, curve(x_, params_final)); % -------------------------------------------------------------------- function varargout = editX_0_Callback(h, eventdata, handles, varargin) handles.new_params(1) = str2num(get(handles.editX_0, 'String')); guidata(gcbo, handles); refresh(handles.axes1, handles.data, handles.new_params); % -------------------------------------------------------------------- function varargout = editK_x_Callback(h, eventdata, handles, varargin) handles.new_params(2) = str2num(get(handles.editK_x, 'String')); guidata(gcbo, handles); refresh(handles.axes1, handles.data, handles.new_params); % -------------------------------------------------------------------- function varargout = editY_0_Callback(h, eventdata, handles, varargin) handles.new_params(3) = str2num(get(handles.editY_0, 'String')); guidata(gcbo, handles); refresh(handles.axes1, handles.data, handles.new_params); % -------------------------------------------------------------------- function varargout = editK_y_Callback(h, eventdata, handles, varargin) handles.new_params(4) = str2num(get(handles.editK_y, 'String')); guidata(gcbo, handles); refresh(handles.axes1, handles.data, handles.new_params); % -------------------------------------------------------------------- function varargout = pushbuttonAuto_Callback(h, eventdata, handles, varargin) handles.new_params = autofit(handles.data, handles.new_params) ; set(handles.editX_0, 'String', num2str(handles.new_params(1))); set(handles.editK_x, 'String', num2str(handles.new_params(2))); set(handles.editY_0, 'String', num2str(handles.new_params(3))); set(handles.editK_y, 'String', num2str(handles.new_params(4))); guidata(gcbo, handles); % -------------------------------------------------------------------- function varargout = pushbuttonDone_Callback(h, eventdata, handles, varargin) %handles.answer = 'yes'; %guidata(h,handles); uiresume(handles.figure1);