Code covered by the BSD License  

Highlights from
YIPLOT : Easy plot

YIPLOT : Easy plot

by

 

YIPLOT makes plot much easier. you can use it to compress a bunch of codes into one single line.

h=yiplot(varargin)
function h=yiplot(varargin)
%yiplot makes plot much easier.
%   H=YIPLOT(X,Y,...)
%   H=YIPLOT(STR,Y)
%   H=YIPLOT(STR,X,Y,...)
%   STR is a charater string made from any elements below, seperated by
%   comma, eg. 'fig=1,lw=4,t=title,x=x axis label'
%
%   fig = #       set # as current figure, same as command figure(#)
%   sub = #      subplot(#)
%   lw = #        set linewidth to #
%   t = text      add title, title('text')
%   t# = text    add title with fontsize #, title('text','fontsize',#)
%   x = text     add X-axis label, xlabel('text')
%   x# = text   add X-axis label with fontsize #, xlabel('text','fontsize',#)
%   y =, y# =   add Y-axis label
%   z =, z# =    add Z-axis label
%   f = #          set fontsize # for all
%   b                set fontweight as bold for all
%   grid            grid on
%
%  EXAMPLE 1:
%  You can produce a graph of sin function with line width of 3, x-axis label of
%  'theta', y-axis label of 'sin(theta)', graph title of 'Plot of sin(theta)' like this:
%   x = -pi:.1:pi;
%   y = sin(x);
%   yiplot('lw=3,x=\theta, y=sin(\theta), t=Plot of sin(\theta)', x, y)
%   
%   EXAMPLE 2:
%   Now set fontsize of x-label and y-label to 16, set fontsize of title to
%   20:
%   yiplot('lw=3,x16=\theta, y16=sin(\theta),t20=Plot of sin(\theta)',x,y)
%
%   EXAMPLE 3:
%   You can also add any arguments that you do in PLOT() command.
%   For example, you can plot red dashed line with star markers like this:
%   yiplot('lw=3,x16=\theta, y16=sin(\theta),t20=Plot of sin(\theta)',x,y,'*r--')
%
%   Copyright 2010 Y. Sui
%   $Date: 2010/9/15 $
%
% See also plot, title, xlabel, ylabel, figure, subplot, grid.
        if ~ischar(varargin{1}) % act as PLOT()
            ht=plot(varargin{:});
            if nargout>0 h=ht;end
            return 
        end        
%         ht=plot(varargin{2:end});
            cmdstr=varargin{1};
            [propname value]=parsecmd(cmdstr);
            propname=strtrim(propname);%remove white spaces.
        
        
        ttl=cellfun(@(x) ['t' num2str(x)], [{[]} num2cell(1:50)],'UniformOutput', false);%title+fontsize, ttl={'t', t1','t2',...'t50'} 
        xl=cellfun(@(x) ['x' num2str(x)], [{[]} num2cell(1:50)],'UniformOutput', false);%xlabel+fontsize, xl={'x', 'x1',xt2',...'x50'} 
        yl=cellfun(@(x) ['y' num2str(x)], [{[]} num2cell(1:50)],'UniformOutput', false);
        zl=cellfun(@(x) ['z' num2str(x)], [{[]} num2cell(1:50)],'UniformOutput', false);

        
        for k=1:length(propname)
            switch propname{k}
                case 'fig'
                   fg=figure(str2double(value{k}));
                case 'sub'
                   subplot(str2double(value{k}));                 
            end
        end

        ht=plot(varargin{2:end});
        
        for k=1:length(propname)
            switch propname{k}
                case 'lw'
                    set(ht,'linewidth',str2double(value{k}));
                case ttl;
                    ftsz=getfontsize(propname{k},2);
                    title(value{k},'fontsize',ftsz);
                case xl
                    ftsz=getfontsize(propname{k},2);
                    xlabel(value{k},'fontsize',ftsz);
                case yl
                    ftsz=getfontsize(propname{k},2);
                    ylabel(value{k},'fontsize',ftsz);
                case zl
                    ftsz=getfontsize(propname{k},2);
                    zlabel(value{k},'fontsize',ftsz);
                case 'f' % Set font size for all
                    set(get(gca,'title'),'fontsize',str2double(value{k}));
                    set(get(gca,'xlabel'),'fontsize',str2double(value{k}));
                    set(get(gca,'ylabel'),'fontsize',str2double(value{k}));                    
                case 'b'%bold
                    set(get(gca,'title'),'fontweight','b');
                    set(get(gca,'xlabel'),'fontweight','b');
                    set(get(gca,'ylabel'),'fontweight','b');
                case 'grid'
                    grid on;
%                 otherwise
%                   warning([propname{k} ' is not defined']);
            end
        end
        
        if nargout>0
            h=ht;
        end
end
function [propname value]=parsecmd(str)
    propname={};
    value={};

    rem=str;
    while true
      [token,rem]=strtok(rem, ',');
      if isempty(token),  break;  end
       [name, rem2]=strtok(token,'=');
       val=strtok(rem2,'=');
       propname=[propname,name];
       value=[value,{val}];
    end
%     cmd={}
%     for k=1:length(propname)
%         idx=strcmp(abbr,propname(k));
%         cmd=[cmd, fullname(idx), values(k)];
%     end
%     cmd
% 

end
function ftsz=getfontsize(propname,pb)
    if length(propname)>1;
        ftsz=str2double(propname(pb:end));
    else
        ftsz=10; %default
    end
end

Contact us