image thumbnail
from mPlot - Enhanced Plotting Engine by Erik Lee
Enhanced plotting engine that extends the basic plotting functionality delivered with Matlab.

mPlot<handle %classdef nameOfClass<inheritedClasses
classdef mPlot<handle %classdef nameOfClass<inheritedClasses
    %% Information
    % Author: Erik Lee
    % Email: leeen82@gmail.com
    % Website: http://matlabmafia.com/?p=21
    % Date: January 2011
    % Visit the website for full tutorials and details regarding this class
    %% Constructor
    methods
        function obj = mPlot() %name of constructor function must match name of class
            obj.buildProperties(); %loads all default property values
            obj.isAutoIncrementing = 0; %Default to off
            obj.isAutoIncrementingPreviousState = 0;
            obj.isFormattingParents = 1; %Default to enabled
            obj.heldMarkerStyle = 'none'; %default to no marker
            obj.isHoldingMarkerStyle = 0; %default to off
            obj.markerStepSize = 1; %default to 1
            obj.isHoldingStateIncrementMarkerSize = 0; %default to off
            obj.currentHeldMarkerSizeToAdd = 0; %initialize to zero
        end %end constructor function
    end %end methods section
    %% Properties
    properties(GetAccess = public, SetAccess = public)
        mEColor; %defines the marker edge colors to use
        mFColor; %defines the marker face colors to use
        mType;   %defines the marker types to use
        mSize;   %defines the marker sizes to use
        lineStyle; %defines the line style
        plotCounter; %this can be incremented externally to determine 
                     %which styles to apply to the line
        isAutoIncrementing; %1 or 0.  Determines if plotCounter should 
                            %automatically increment after each line is
                            %drawn to the current axes.
        isFormattingParents; %1 or 0. Determines if formatting will be 
                             %applied to the axes and figure containing the
                             %line.
        isHoldingMarkerStyle; %1 or 0.  Determines if the marker style is 
                              % currently being held.
        heldMarkerStyle;      %Specifies the marker style to be applied when 
                              %isHoldingMarkerStyle is enabled (set to 1).
        markerStepSize; %amount of marker size to add.  Basically 
                                %an increment amount
    end
    
    %% Private Properties
    properties(GetAccess = public, SetAccess = private)
        isAutoIncrementingPreviousState; %Holds the last state of the 
                                         %isAutoIncrementing property.
                                         %This is useful for disabling and
                                         %enabling auto incrementing when
                                         %the object is set to certain
                                         %states
    end
    properties(GetAccess = public, SetAccess = protected)
        isHoldingStateIncrementMarkerSize; %determines if the state is 
                                           %held with the marker size 
                                           %incremented
        currentHeldMarkerSizeToAdd; %holds marker size to add
    end
    %% Set/Get Methods for Properties
    methods
        function  set.isHoldingMarkerStyle(obj,value)
            switch value
                case 0
                obj.isHoldingMarkerStyle = value; %set to 0, disables 
                obj.isAutoIncrementing = obj.isAutoIncrementingPreviousState;
                case 1
                obj.isHoldingMarkerStyle = value; %set to 1, enables
                %store previous state of auto incrementing
                obj.isAutoIncrementingPreviousState = obj.isAutoIncrementing;
                %enable auto-incrementing
                obj.isAutoIncrementing = 1;
                otherwise
                obj.isHoldingMarkerStyle = 0;  %set to 0, disable b/c 
                                               %value passed in was not of 
                                               %the proper form
                obj.isAutoIncrementing = obj.isAutoIncrementingPreviousState;
            end
            %Next, check if marker style if valid
            isValidMarker = obj.checkIfValidMarker(obj.heldMarkerStyle);
            if(~isValidMarker) %if it is not valid
               obj.heldMarkerStyle = 'none'; % then set to none
            end
            
        end
        function val = get.isHoldingMarkerStyle(obj)
           %This method not required. Added to be thorough and consistent
            val = obj.isHoldingMarkerStyle; %just return the value.
        end
    end
    %% Public Methods - General (Accessible by users of the class)
    methods(Access = public)
        function plot(obj,varargin)
        %This function performs plotting.
        %The inputs are x1, y1, x2, y2,.....xn,yn.
        %Lines are placed on the current axes.  Hence the user of the class
        %has control over which axes the lines get drawn on.
        obj.errorTrapPlotCounter(); %check plotCounter value
            nLines = floor(nargin/2); 
            %loop through all of the lines
            for lineCounter = 1:nLines
                if(obj.isHoldingMarkerStyle) %If holding marker
                    currentMarker = obj.heldMarkerStyle; %use held marker
                else %else use the next pre-defined marker
                    currentMarker = obj.mType{lineCounter+obj.plotCounter};
                end
                if(obj.isHoldingStateIncrementMarkerSize)
                   obj.currentHeldMarkerSizeToAdd = obj.currentHeldMarkerSizeToAdd + ...
                       obj.markerStepSize;
                end
                xIndex = (lineCounter-1)*2 + 1; %Holds current xIndex.
                yIndex = xIndex + 1; %Holds current yIndex.
                hold on; %Hold all lines on the plot and add next one.
                plot(varargin{xIndex},varargin{yIndex},...
                    'marker',currentMarker,...
                    'markerSize',obj.mSize(lineCounter + obj.plotCounter) + obj.currentHeldMarkerSizeToAdd,...
                    'markerEdgeColor',obj.mEColor(lineCounter + obj.plotCounter,:),...
                    'markerFaceColor',obj.mFColor(lineCounter + obj.plotCounter,:),...
                    'LineStyle',obj.lineStyle{lineCounter + obj.plotCounter},...
                    'color',obj.mEColor(lineCounter + obj.plotCounter,:));
            end %end for loop
            
            %Check if auto-incrementing is enabled.
            if(obj.isAutoIncrementing && ~obj.isHoldingStateIncrementMarkerSize)
               obj.plotCounter = obj.plotCounter + nLines-1;
               obj.incrementPlotCounter(); %call method in case we decide 
                                           %to perform some actions within 
                                           %the method later.
            end %end if(obj.isAutoIncrementing)
            
            %Check if formatting parents is enabled
            if(obj.isFormattingParents)
               obj.formatParents(); 
            end
        end %end function
        function buildProperties(obj)
            nom = 255;
            obj.mEColor(1,1:3) = [28 57 187]./ nom;   % Persian Blue
            obj.mFColor(1,1:3) = [135 206 235]./nom;  % Sky Blue
            obj.mType{1} = 'o'; 		              % Circle
            obj.mSize(1) = 5;
            obj.lineStyle{1} = '-';
            
            obj.mEColor(2,:) = [50 205 50]./ nom;   % Lime Green
            obj.mFColor(2,:) = [204 255 0]./nom;    % Electric Lime
            obj.mType{2} = 's'; 		            % Square
            obj.mSize(2) = 5;
            obj.lineStyle{2} = '-';
            
            obj.mEColor(3,:) = [200 8 21]./ nom;    % Venetian Red
            obj.mFColor(3,:) = [255 0 0]./nom;      % Red
            obj.mType{3} = 'd'; 		            % Diamond
            obj.mSize(3) = 5;
            obj.lineStyle{3} = '-';
            
            obj.mEColor(4,:) = [102 0 153]./ nom;     % Generic Purple
            obj.mFColor(4,:) = [221 0 255]./nom;      % Psychedelic Purple
            obj.mType{4} = '^'; 		              % Triangle
            obj.mSize(4) = 5;
            obj.lineStyle{4} = '-'; 
            
            obj.mEColor(5,:) = [54 69 79]./ nom;      % Charcoal
            obj.mFColor(5,:) = [191 191 191]./nom;    % Silver
            obj.mType{5} = 's'; 		              % Square
            obj.mSize(5) = 5;
            obj.lineStyle{5} = '-'; 
            
            obj.mEColor(6,:) = [220 132 0]./ nom;     % Fulvous
            obj.mFColor(6,:) = [253 238 0]./nom;      % Aureolin
            obj.mType{6} = 'd'; 		              % Diamond
            obj.mSize(6) = 5;
            obj.lineStyle{6} = '-'; 
            
            obj.mEColor(7,:) = [46 139 87]./ nom;     % Sea Green
            obj.mFColor(7,:) = [84 255 159]./nom;     % Green
            obj.mType{7} = 'o'; 		              % Circle
            obj.mSize(7) = 5;
            obj.lineStyle{7} = '-'; 
            
            obj.mEColor(8,:) = [222 49 99]./ nom;     % Cerise
            obj.mFColor(8,:) = [255 105 180]./nom;    % Hot Pink
            obj.mType{8} = 's'; 		              % Square
            obj.mSize(8) = 5;
            obj.lineStyle{8} = '-'; 
            
            obj.mEColor(9,:) = [0 204 204]./ nom;     % Robin Egg Blue
            obj.mFColor(9,:) = [0 255 255]./nom;      % Aqua
            obj.mType{9} = 'd'; 		              % Diamond
            obj.mSize(9) = 5;
            obj.lineStyle{9} = '-'; 
            
            obj.mEColor(10,:) = [39 139 34]./ nom;     % Forest Green
            obj.mFColor(10,:) = [154 205 50]./nom;     % Yellow Green
            obj.mType{10} = 'o'; 		               % Circle
            obj.mSize(10) = 5;
            obj.lineStyle{10} = '-'; 
            
            obj.plotCounter = 0; %initialize the plot counter
        end %end buildProperties function
        %This function will format the current axes and figure
        function formatParents(obj)
            axesHandle = gca; %get current axes handle
            set(axesHandle,'Color',[211 211 211]./255); %set axes background color
            set(axesHandle,'drawMode','fast'); %alters rendering order
            set(axesHandle,'fontSize',14); %set axes font size
            set(axesHandle,'gridLineStyle','--'); %define gridline style
            set(axesHandle,'minorGridLineStyle',':'); %set minor gridline style
            set(axesHandle,'TickDir','out'); % out or in - set tick direction
            set(gcf,'color',[1 1 1]); %set figure background to white
            grid on; %turn the grid on
        end % ends the formatGCA function
    end %end public methods block
    %% Public Methods - For Controlling plotCounter
    methods(Access = public)
        function resetPlotCounter(obj)
            obj.plotCounter = 0; %set plotCounter to zero
        end
        function incrementPlotCounter(obj)
            %Add one to plotCounter if a color definition exists for the
            %incremented value
            if(obj.plotCounter < length(obj.mEColor)) obj.plotCounter = obj.plotCounter + 1; end
        end
        function decrementPlotCounter(obj)
            %Subtract one from plotCounter if a color definition exists for
            %the decremented value.
            if(obj.plotCounter > 0) obj.plotCounter = obj.plotCounter - 1; end
        end
        function errorTrapPlotCounter(obj) % Check plotCounter
            if(obj.plotCounter > length(obj.mType)-1) %ensure that plotCounter
                                                      %isn't too large
               obj.resetPlotCounter();  %reset plot counter
            end
        end
    end
    %% Public Methods - For Controlling Line Properties
    methods(Access = public)
        function setCurrentMarkerType(obj,markerType)
           %markerType = Basic marker type options defined by matlab.
           %+,o,*,.,x,s,square,diamond,d,^,v,>,<,pentagram,p,hexagram,none
           validMarkers = {'+','o','*','.','x','s','square','diamond','d',...
               '^','v','>','<','pentagram','p','hexagram','none'};
           checkValue = find(ismember(validMarkers,markerType)==1); 
           if(~isempty(checkValue)) %if empty then invalid marker
               obj.mType{obj.plotCounter + 1} = markerType; 
           end
        end
        function setCurrentMarkerSize(obj,markerSize)
            %markerSize = scalar. Sets the current marker size.
           obj.mSize(obj.plotCounter + 1) = markerSize; 
        end
        function setCurrentLineStyle(obj,lineStyle)
            %lineStyle = Basic linestyle options provided by matlab.
            %-,--,:,-.,none
            validLineStyles = {'-','--',':','-.','none'};
            checkValue = find(ismember(validLineStyles,lineStyle)==1); 
           if(~isempty(checkValue)) %if empty then invalid marker
               obj.lineStyle{obj.plotCounter + 1} = lineStyle; 
           end
        end
        function setCurrentMarkerEdgeColor(obj,color)
            %color should be a 1x3 array of values defining the RGB values.
            [r,c] = size(color);
            if(r>c) color = color'; end %get matrix dimensions correct
            obj.mEColor(obj.plotCounter + 1,1:3) = color; 
        end 
    end
    %% Public Methods - Incrementing Marker Size while holding line definition
    methods(Access = public)
        function holdStateIncrementMarkerSizeOn(obj)
           %Enable marker size enlarging while holding all other line
           %definition properties.
           obj.isHoldingStateIncrementMarkerSize = 1;
           obj.currentHeldMarkerSizeToAdd = obj.markerStepSize;
        end
        function holdStateIncrementMarkerSizeOff(obj)
           %Disable marker size enlarging while holding all other line
           %definition properties.
           obj.isHoldingStateIncrementMarkerSize = 0;
           obj.currentHeldMarkerSizeToAdd = 0;
        end
    end
    %% Public Methods - Access All Marker Sizes
    methods(Access= public)
        function setGlobalMarkerSize(obj,markerSize)
           % Change all of the marker sizes for each unique line style
           obj.mSize = markerSize.*ones(1,length(obj.mType)); %set all sizes
        end
    end
    %% Helper Methods
    methods(Access = public)
        function isValidMarker = checkIfValidMarker(obj,markerType)
           %markerType = Basic marker type options defined by matlab.
           %+,o,*,.,x,s,square,diamond,d,^,v,>,<,pentagram,p,hexagram,none
           validMarkers = {'+','o','*','.','x','s','square','diamond','d',...
               '^','v','>','<','pentagram','p','hexagram','none'};
           checkValue = find(ismember(validMarkers,markerType)==1); 
           if(~isempty(checkValue)) %if empty then invalid marker
              isValidMarker = 1; 
           else
              isValidMarker = 0;
           end
        end
    end
    %% Public Methods - To Adjust Color Schemes
    methods(Access = public)
        function setColorSchemeJet(obj)
            obj.mFColor = jet(10);
            darkVals = jet(10) - 0.3;
            darkVals(darkVals<0) = 0;
            obj.mEColor = darkVals;
        end
        function setColorSchemeHot(obj)
            obj.mFColor = hot(10);
            darkVals = hot(10) - 0.3;
            darkVals(darkVals<0) = 0;
            obj.mEColor = darkVals;
        end
        function setColorSchemeHSV(obj)
            obj.mFColor = hsv(10);
            darkVals = hsv(10) - 0.3;
            darkVals(darkVals<0) = 0;
            obj.mEColor = darkVals;
        end
        function setColorSchemeCool(obj)
            obj.mFColor = cool(10);
            darkVals = cool(10) - 0.3;
            darkVals(darkVals<0) = 0;
            obj.mEColor = darkVals;
        end
        function setColorSchemeLines(obj)
            obj.mFColor = lines(10);
            darkVals = lines(10) - 0.3;
            darkVals(darkVals<0) = 0;
            obj.mEColor = darkVals;
        end
        function setColorSchememPlot(obj)
            obj.buildProperties();
        end
    end
end %end class definition

Contact us