function [style,color,marker,msg] = colstyle(a)
%COLSTYLE Parse color and style from string.
%   [L,C,M,MSG] = COLSTYLE('linespec') parses the line specification
%   'linespec' and returns the linetype part in L, the color part in C,
%   and the marker part in M.  L,C and M are empty arguments for
%   parts that are not specified or if there is a parsing error.  In
%   case of error, MSG will contain the error message string.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.6 $  $Date: 1997/11/21 23:46:02 $

if nargin ~= 1 | (nargin ==1 & ~isstr(a))
    error('Requires one string argument.');
end

valid_colors = 'ymrgbwkc'; % and don't forget c followed by an integer
valid_lines = '-:'; % don't forget -- and -.
valid_markers = '+o*.xsdv^><ph';

style = ''; color = ''; marker = ''; msg = [];

if isempty(a), return, end

% Look for bad characters
[aa,b] = meshgrid(a,[valid_colors valid_lines valid_markers '0123456789']);
if any(all(aa~=b)), msg = 'Bad characters in LineSpec string.'; return, end

% Look for color
[aa,c] = meshgrid(a,valid_colors);
[kr,kc] = find(aa==c);
if ~isempty(kc), % found a color
  kc = min(kc);
  color = a(kc);
  if color == 'c' & kc<length(a), % Check for c followed by integer
    d = a(kc+1:end) >= '0' & a(kc+1:end) <= '9';
    d = find(d(1:min(length(d),2)));
    if ~isempty(d), color = [color a(d+kc)]; end
  end
end

% Look for style
[aa,l] = meshgrid(a,valid_lines);
[kr,kc] = find(aa==l);
if ~isempty(kc), % Found a line style
  kc = min(kc);
  style = a(kc);
  if style == '-' & kc<length(a), % Check for -. and --
    if a(kc+1)=='-',
      style = '--';
    elseif a(kc+1)=='.'
      style = '-.';
      a(kc+1)=' '; % Make sure '.' doesn't show up as a marker.
    end
  end
end

% Look for marker
[aa,m] = meshgrid(a,valid_markers);
[kr,kc] = find(aa==m);
if ~isempty(kc), % Found a line style
  kc = min(kc);
  marker = a(kc);
end

if length(style)+length(color)+length(marker) ~= length(a)
  msg = 'Invalid LineSpec string.';
end
