image thumbnail

Multi Plot with marker spacing

by

 

plot large 2D data set with custom marker spacing

multi_plot(varargin)
%% mutli_plot
% Writen by:    Scott T. Block
% Date:         5th of Oct. 2013
% Version:      1
%
%%% PROGRAM USE %%%
% This scrip is used to generate an over lap of downsampled plot with
% markers on the orginal line plot.  To be used with 2D plots with large 
% number of points, but make markers at a nice spacing.  
%
%%% ASSUMPTIONS %%%
% assuming all cells are empty or row or column vectors, i.e. 1xn cells
% placing more than one x and y onto same plot, input must be in pairs
%
%%% OLD SPECFICATIONS %%%
%   LineStyle   -   --  :   -. none
%   LimeWidth   enter an scalar 
%   Color       [r,g,b] where r,g,b are [0 to 1] is the ratio of r,g,b 
%                       ~~~ OR ~~~
%               b g r c m y k w, see plot doc
%   Marker
%   MarkerEdgeColor
%   MarkerFaceColor
%   MarkerSize
%
%%% ADDING A NEW SPECIFICATION %%%
%       'MarkerSpacing',N   ---> where N is an integer > 0.  This is the
%                               spacing between markers desired
%   >> multi_plot(x0,y0,cell_0,x1,y1,cell_1,x2,y2,cell_2,...)
%
%     cell_0 = {'Color','c','LineStyle','-','LineWidth',2,'Marker','*'};
%     cell_1 = {'Color','r','LineStyle','-.','LineWidth',2,'Marker','v', ...
%         'MarkerSpacing',500};
%     cell_2 = {'Color','g','LineStyle','-','LineWidth',2,'Marker','^', ...
%         'MarkerSpacing',10,'MarkerSize',10};
%     %cell_3 = {'Color','m','LineStyle','-','LineWidth',2};
%     cell_3 = {};
%     cell_4 = {'Color',[0,0,1],'LineStyle','--','LineWidth',2};
%     multi_plot(time,V_out,cell_0, ...
%                time,V_sim,cell_1, ...
%                time_Cad,V_out_Cad,cell_2, ...
%                time,V_B,cell_3, ...
%                t_s,V_p,cell_4);
%
% cell_0, cell_1, cell_2, ... are used to set how each plot will show up
%
% See also rmz, cell, cell2mat, figure, plot, varargin, celldisp, nargin,
% downsample

function multi_plot(varargin)
    L = nargin; % number of arguments in
    h = zeros(L/3,1);
    SN = h;             % skip number amount
    WE_GOT_MARKERS = h; % assuming no markers to save time
                        % 0 = no markers
                        % 1 = markers with spacing
                        % -1 = markers without spacing

    %% first round of plotting
    % every thing that is not using MarkerSpacing will be ploted here
    for k = 1:3:L
        temp_cell = {}; % empting cell, used to hold all line styles
        L_cell = length(varargin{k+2}); % length of cell with line styles
        n = 1;  % temp cell counter
        HC = ceil(k/3); % handle count
        for m = 1:L_cell
            % varargin is a cell, and I'm also looking with in a cell,
            % {k+2} is the place I'm looking with in my miain well and
            % (m) is with in my sub cell
            if(strcmp('LineStyle',varargin{k+2}(m)) || ...
                    strcmp('LineWidth',varargin{k+2}(m)) || ...
                    strcmp('Color',varargin{k+2}(m)))
                temp_cell(n) = varargin{k+2}(m);  %#ok<AGROW> % line call
                n = n + 1;
                temp_cell(n) = varargin{k+2}(m+1);%#ok<AGROW> % line call
                n = n + 1;
            elseif(strcmp('MarkerSpacing',varargin{k+2}(m)))
                WE_GOT_MARKERS(HC) = 1; % we got markers and going to space
                SN(HC) = cell2mat(varargin{k+2}(m+1));  % saving skip amount
            elseif(strcmp('Marker',varargin{k+2}(m)) || ...
                    strcmp('MarkerEdgeColor',varargin{k+2}(m)) || ...
                    strcmp('MarkerFaceColor',varargin{k+2}(m)) || ...
                    strcmp('MarkerSize',varargin{k+2}(m)))
                if(WE_GOT_MARKERS(HC)==0)
                    WE_GOT_MARKERS(HC) = -1; % we got markers and not sure 
                end
                temp_cell(n) = varargin{k+2}(m);  %#ok<AGROW> % line call
                n = n + 1;
                temp_cell(n) = varargin{k+2}(m+1);%#ok<AGROW> % line call
                n = n + 1;
            end
        end
        % passing x and y into temp variables 
        tempx = varargin{k};
        tempy = varargin{k+1};
        if(WE_GOT_MARKERS(HC) == 1)
            % assigning the same spot to all values in the array, so it
            % will show up as a single spot in the plot, but still have the
            % correct handle, then for this line, I'll have to replot two
            % lines, 1 with just markers, then the other with the line
            tempx(:) = varargin{k}(1);  
            tempy(:) = varargin{k+1}(1);
        end
        % first round of actually plotting
        if(k == 1)
            h(HC) = plot(tempx,tempy); hold on;
        else
            h(HC) = plot(tempx,tempy);
        end
        % only applying LineStyle, LineWidth, and Coloring to plot
        set(h(HC),temp_cell{:});
    end

    %% Second Round of Plotting
    % Now plotting the lines that are using MarkerSpacing
    % which plot is actually using marker spacings?
    IDX = rmz((1:length(WE_GOT_MARKERS))'.*(WE_GOT_MARKERS==1));% plot index 
    SN = rmz(SN);   % removing all non-zeros, how much will skip 
    PN = length(IDX);   % Plot Number that will use Marker Spacing
    h2 = zeros(2*PN,1);  % handles for this round
    DONE = 'NO';
    m = 1;  % plot counter
    while(~isempty(IDX) && strcmp(DONE,'NO'))
        % so IDX represents which plots are using spacings, and recall that
        % every plot has 3 enteries.  So first plot 1, had spot 1, 2, and 3
        % of varargin, plot 2 would have 4, 5, 6... so the starting point
        % would be the plot number * 3 - 2, and the ending point would be
        % plot number * 3
        k = (IDX(m) * 3) - 2;   % plot_number * 3 - 2, i.e. starting point
        n = (IDX(m) * 2) - 1;   % handle counter
        % now time to plot
        h2(n) = plot(varargin{k},varargin{k+1});    % line plot
        PHASE = round((SN(m)-1) / PN) * (m-1);      % phase for marker plot
        SKIP = SN(m);                               % skip amount for plot
        % marker plot
        h2(n+1) = plot(downsample(varargin{k},SKIP,PHASE), ...
                       downsample(varargin{k+1},SKIP,PHASE));
        
        % now 
        L_cell = length(varargin{k+2}); % length of cell with line styles
        q = 1;  % line only counter
        r = 1;  % marker only counter
        for p = 1:L_cell
            if(strcmp('LineStyle',varargin{k+2}(p)))
                line_cell(q) = varargin{k+2}(p);  %#ok<AGROW> % line call
                q = q + 1;
                line_cell(q) = varargin{k+2}(p+1);%#ok<AGROW> % line call
                q = q + 1;
                mark_cell{r} = 'LineStyle';     %#ok<AGROW> % line call
                r = r + 1;
                mark_cell{r} = 'none';          %#ok<AGROW> % line call
                r = r + 1;
            elseif(strcmp('Color',varargin{k+2}(p)) || ...
                    strcmp('LineWidth',varargin{k+2}(p)))
                line_cell(q) = varargin{k+2}(p);  %#ok<AGROW> % line call
                q = q + 1;
                line_cell(q) = varargin{k+2}(p+1);%#ok<AGROW> % line call
                q = q + 1;
                mark_cell(r) = varargin{k+2}(p);  %#ok<AGROW> % line call
                r = r + 1;
                mark_cell(r) = varargin{k+2}(p+1);%#ok<AGROW> % line call
                r = r + 1;
            elseif(strcmp('Marker',varargin{k+2}(p)) || ...
                strcmp('MarkerEdgeColor',varargin{k+2}(p)) || ...
                strcmp('MarkerFaceColor',varargin{k+2}(p)) || ...
                strcmp('MarkerSize',varargin{k+2}(p)))
                mark_cell(r) = varargin{k+2}(p);  %#ok<AGROW> % line call
                r = r + 1;
                mark_cell(r) = varargin{k+2}(p+1);%#ok<AGROW> % line call
                r = r + 1;
            end
        end
        % setting how the plots should look
        set(h2(n),line_cell{:});
        set(h2(n+1),mark_cell{:});
        
        % updating and incrementing
        m = m + 1;  % plot counter, incremented
        if(m > PN)
            DONE = 'YES';
        else
            DONE = 'NO';
        end
    end
    hold off;
end

Contact us