MATLAB Object-Oriented Programming

(Joyce) #1

  • The callback function for the slider accepts the three required arguments — a class
    instance, the handle of the event source, and the event data. The event data argument
    is empty and not used.

  • The uicontrol callback uses dot notation to reference the callback
    method: ...'Callback',@obj.sliderCallback.


classdef SeaLevelSlider < handle
properties
Figure
Axes
CLimit
end

methods
function obj = SeaLevelSlider(x,map)
obj.Figure = figure('Colormap',map,...
'Position',[100,100,560,580],...
'Resize','off');
obj.Axes = axes('DataAspectRatio',[1,1,1],...
'XLimMode','manual','YLimMode','manual',...
'Parent',obj.Figure);
image(x,'CDataMapping','scaled',...
'Parent',obj.Axes);
obj.CLimit = get(obj.Axes,'CLim');
uicontrol('Style','slider',...
'Parent',obj.Figure,...
'Max',obj.CLimit(2)-10,...
'Min',obj.CLimit(1)-1,...
'Value',obj.CLimit(1),...
'Units','normalized',...
'Position',[0.9286,0.1724,0.0357,0.6897],...
'SliderStep',[0.003,0.005],...
'Callback',@obj.sliderCallback);
end

function sliderCallback(obj,src,~)
minVal = get(src,'Value');
maxVal = obj.CLimit(2);
obj.Axes.CLim = [minVal maxVal];
end
end
end

9 Methods — Defining Class Operations

Free download pdf