0001 function highlight(mfile,options,outfile)
0002 %HIGHLIGHT - Syntax Highlighting of Matlab M-files in HTML, LaTeX, RTF and XML.
0003 %  HIGHLIGHT(MFILE) takes an M-file MFILE in input and writes on disk an HTML
0004 %  file with the same basename ('foo.m' => 'foo.html') adding colored syntax 
0005 %  highlighting on comment, string and Matlab keyword elements.
0006 %  HIGHLIGHT(MFILE,OPTIONS) with OPTIONS being string 'html', 'xhtml', 'tex', 
0007 %  'rtf' or 'xml', allows to choose the output format between HTML, XHTML, LaTeX
0008 %  RTF or XML. The same rule is used to determine the name of the output file.
0009 %  HIGHLIGHT(MFILE,OPTIONS) allows to specify some options in a structure:
0010 %     options.type - Output file type 
0011 %             [ {'html'} | 'tex' | 'rtf' | 'xml' | 'xhtml']
0012 %     options.tabs - Replace '\t' in source code by n white space
0013 %                           [ 0 ... {4}  ... n]
0014 %     options.linenb - Display line number in front of each line [ 0 | {1} ]
0015 %  HIGHLIGHT(MFILE,OPTIONS,OUTFILE) allows to specify the name of the output 
0016 %  file. OUTFILE can also be a file handle. In that case, no header will be 
0017 %  written, only the highlighted Matlab code will be sent to the OUTFILE stream.
0018 
0019 %  Output file can be customized (font style, font color, font size, ...):
0020 %     o HTML: use CSS (Cascading Style Sheets) to define 'comment', 'string', 
0021 %       'keyword', 'cont' and 'code' SPAN elements and 'mcode' PRE tag.
0022 %     o LaTeX: packages 'alltt' and 'color' are required, you can modify colors
0023 %       in defining colors 'string', 'comment' and 'keyword' using command
0024 %                      \definecolor{mycolor}{rgb}{a,b,c}
0025 %     o RTF: Colors are defined in the Color Table at the beginning of the
0026 %       document. See Rich Text Format Specifications for more details:
0027 %       <http://msdn.microsoft.com/library/en-us/dnrtfspec/html/rtfspec.asp>
0028 %     o XML: you will find the DTD of the resulting XML file in matlab.dtd
0029 %       You can then use XSL to transform your XML file in what you want.
0030 %       For example, mat2html.xsl transforms your XML file in HTML as it would
0031 %       be if you would have used highlight.m to to so.
0032 %       On Matlab 6.5, the command is:
0033 %               xslt highlight.xml mat2html.xsl highlight.html
0034 %       On Linux, using libxslt <http://xmlsoft.org/XSLT/>, the command is: 
0035 %               xsltproc -o highlight.html mat2html.xsl highlight.xml
0036 
0037 %  Copyright (C) 2003 Guillaume Flandin
0038 %  $Revision: 1.1 $Date: 2003/09/07 15:39:33 $
0039 
0040 %  This program is free software; you can redistribute it and/or
0041 %  modify it under the terms of the GNU General Public License
0042 %  as published by the Free Software Foundation; either version 2
0043 %  of the License, or any later version.
0044 % 
0045 %  This program is distributed in the hope that it will be useful,
0046 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0047 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0048 %  GNU General Public License for more details.
0049 % 
0050 %  You should have received a copy of the GNU General Public License
0051 %  along with this program; if not, write to the Free Software
0052 %  Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA.
0053 
0054 % Suggestions for improvement and fixes are always welcome, although no
0055 % guarantee is made whether and when they will be implemented.
0057 
0058 %- Set up options
0059 error(nargchk(1,3,nargin));
0060 
0061 opt = struct('type',   'html', ...
0062              'tabs',   4, ...
0063              'linenb', 1, ...
0064              'indent', 0);
0065 if nargin >= 2
0066     if isstruct(options)
0067         names = fieldnames(options);
0068         for i=1:length(names)
0069             opt = setfield(opt,names{i},getfield(options,names{i}));
0070         end
0071     elseif ischar(options)
0072         opt.type = options;
0073     else
0074         error('Bad input argument.');
0075     end
0076 end
0077 if strcmp(lower(opt.type),'latex'), opt.type = 'tex'; end
0078 if strcmp(lower(opt.type),'xml'), opt.linenb = 1; end
0079 
0080 %- If no output filename is provided, one is chosen according to mfile
0081 if nargin < 3
0082     [pathstr, name, ext] = fileparts(mfile);
0083     outfile = fullfile(pathstr, [name, '.' lower(opt.type)]);
0084 end
0085 
0086 %- If an output filename is provided, a standard header is created
0087 if ischar(outfile)
0088     outfid = fopen(outfile,'wt');
0089     if outfid == -1
0090         error(sprintf('Cannot open ',outfile));
0091     end
0092     feval([lower(opt.type) '_file_start'],outfid,mfile);
0093 %- Otherwise a file handle is provided
0094 else
0095     outfid = outfile;
0096 end
0097 
0098 %- Open the Matlab mfile to be highlighted
0099 mfid = fopen(mfile,'rt');
0100 if mfid == -1
0101     error(sprintf('Cannot open ',mfile));
0102 end
0103 
0104 %- Write the syntax highlighted mfile code in the output file
0105 write_highlighted_code(mfid,outfid,opt)
0106 
0107 %- Close the Matlab mfile and potentially the output file
0108 fclose(mfid);
0109 if ischar(outfile), 
0110     feval([lower(opt.type) '_file_end'],outfid);
0111     fclose(outfid); 
0112 end
0113 
0114 %===============================================================================
0115 function write_highlighted_code(mfid,outfid,opt)
0116     matlabKeywords = getMatlabKeywords;
0117     mKeySort       = getMatlabKeywordsSorted;
0118     out_format     = feval([lower(opt.type) '_format']);
0119     strtok_delim   = sprintf(' \t\n\r(){}[]<>+-*~!|\\@&/.,:;="''%');
0120     
0121     fprintf(outfid,out_format.pre_start);
0122     nbline = 1;
0123     nbblanks = 0;
0124     flagnextline = 0;
0125     while 1
0126         tline = fgetl(mfid);
0127         if ~ischar(tline), break, end
0128         tline = feval([lower(opt.type) '_special_char'],horztab(tline,opt.tabs));
0129         %- Display the line number at each line
0130         if opt.linenb
0131             fprintf(outfid,out_format.nb_line,nbline);
0132             nbline = nbline + 1;
0133         end
0134         %- Remove blanks at the beginning of the line
0135         if opt.indent
0136             tline = fliplr(deblank(fliplr(tline)));
0137         end
0138         nbblanks = nbblanks + flagnextline;
0139         flagnextline = 0;
0140         %- Split code into meaningful chunks
0141         splitc = splitcode(tline);
0142         newline = '';
0143         for i=1:length(splitc)
0144             if isempty(splitc{i})
0145             elseif splitc{i}(1) == ''''
0146                 newline = [newline sprintf(out_format.string,splitc{i})];
0147             elseif splitc{i}(1) == '%'
0148                 newline = [newline sprintf(out_format.comment,splitc{i})];
0149             elseif ~isempty(strmatch('...',splitc{i}))
0150                 newline = [newline sprintf(out_format.cont,'...')];
0151                 if ~isempty(splitc{i}(4:end))
0152                     newline = [newline sprintf(out_format.comment,splitc{i}(4:end))];
0153                 end
0154             else
0155                 %- Look for Matlab keywords
0156                 r = splitc{i};
0157                 stringcode = '';
0158                 while 1
0159                     [t,r,q] = strtok(r,strtok_delim);
0160                     stringcode = [stringcode q];
0161                     if isempty(t), break, end;
0162                     isNextIncr  = any(strcmp(t,mKeySort.nextincr));
0163                     isNextIncr2 = any(strcmp(t,mKeySort.nextincr2));
0164                     isCurrDecr  = any(strcmp(t,mKeySort.currdecr));
0165                     isNextDecr  = any(strcmp(t,mKeySort.nextdecr));
0166                     isOther     = any(strcmp(t,mKeySort.other));
0167                     if isNextDecr % if strcmp(t,'end')
0168                         % 'end' is keyword or array subscript ?
0169                         rr = fliplr(deblank(fliplr(r)));
0170                         icomma = strmatch(',',rr);
0171                         isemicolon = strmatch(';',rr);
0172                         if ~(isempty(rr) | ~isempty([icomma isemicolon]))
0173                             isNextDecr = 0;
0174                         else
0175                             nbblanks = nbblanks - 1;
0176                             flagnextline = flagnextline + 1;
0177                         end
0178                         % TODO % false detection of a([end,1])
0179                     end
0180                     if isNextIncr, flagnextline = flagnextline + 1; end
0181                     if isNextIncr2, flagnextline = flagnextline + 2; end
0182                     if isNextDecr, flagnextline = flagnextline - 1; end
0183                     if isCurrDecr, nbblanks = nbblanks - 1; end
0184                     % if any(strcmp(t,matlabKeywords))
0185                     if isNextIncr | isNextIncr2 |isCurrDecr | isNextDecr | isOther
0186                         if ~isempty(stringcode)
0187                             newline = [newline sprintf(out_format.code,stringcode)];
0188                             stringcode = '';
0189                         end
0190                         newline = [newline sprintf(out_format.keyword,t)];
0191                     else
0192                         stringcode = [stringcode t];
0193                     end
0194                 end
0195                 if ~isempty(stringcode)
0196                     newline = [newline sprintf(out_format.code,stringcode)];
0197                 end
0198             end
0199         end
0200         if ~opt.indent, nbblanks = 0; end
0201         %- Print the syntax highlighted line
0202         fprintf(outfid,out_format.line,[blanks(nbblanks*opt.tabs) newline]);
0203     end
0204     fprintf(outfid,out_format.pre_end);
0205 
0206 %===============================================================================
0207 %                                  HTML FORMAT                                 %
0208 %===============================================================================
0209 function html_file_start(fid,mfile)
0210     fprintf(fid,[ ...
0211     '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n' ...
0212     '\t"http://www.w3.org/TR/REC-html40/loose.dtd">\n' ...
0213     '<html>\n<head>\n<title>%s</title>\n' ...
0214     '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n' ...
0215     '<meta name="generator" content="highlight.m &copy; 2003 Guillaume Flandin">\n' ...
0216     '<style type="text/css">\n' ...
0217     '  .comment {color: #228B22;}\n' ...
0218     '  .string {color: #B20000;}\n' ...
0219     '  .keyword, .cont {color: #0000FF;}\n' ...
0220     '  .cont {text-decoration: underline;}\n' ...
0221     '  .code {color: #000000;}\n' ...
0222     '</style>\n' ...
0223     '</head>\n<body>\n'],mfile);
0224 
0225 %-------------------------------------------------------------------------------
0226 function html_file_end(fid)
0227     fprintf(fid,'\n</body>\n</html>');
0228 
0229 %-------------------------------------------------------------------------------
0230 function format = html_format
0231     format.string    = '<span class="string">%s</span>';
0232     format.comment   = '<span class="comment">%s</span>';
0233     format.code      = '%s'; %'<span class="code">%s</span>';
0234     format.keyword   = '<span class="keyword">%s</span>';
0235     format.cont      = '<span class="cont">%s</span>';
0236     format.pre_start = '<pre class="mcode">';
0237     format.pre_end   = '</pre>\n';
0238     format.nb_line   = '%04d ';
0239     format.line      = '%s\n';
0240 
0241 %-------------------------------------------------------------------------------
0242 function str = html_special_char(str)
0243     %- See http://www.w3.org/TR/html4/charset.html#h-5.3.2
0244     str = strrep(str,'&','&amp;');
0245     str = strrep(str,'<','&lt;');
0246     str = strrep(str,'>','&gt;');
0247     str = strrep(str,'"','&quot;');
0248 
0249 %===============================================================================
0250 %                                  XHTML FORMAT                                %
0251 %===============================================================================
0252 function xhtml_file_start(fid,mfile)
0253     fprintf(fid,[ ...
0254     '<?xml version="1.0"?>\n' ...
0255     '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' ...
0256     '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' ...
0257     '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">\n' ...
0258     '<head>\n<title>%s</title>\n' ...
0259     '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n' ...
0260     '<meta name="generator" content="highlight.m &copy; 2003 Guillaume Flandin" />\n' ...
0261     '<style type="text/css">\n' ...
0262     '  .comment {color: #228B22;}\n' ...
0263     '  .string {color: #B20000;}\n' ...
0264     '  .keyword, .cont {color: #0000FF;}\n' ...
0265     '  .cont {text-decoration: underline;}\n' ...
0266     '  .code {color: #000000;}\n' ...
0267     '</style>\n' ...
0268     '</head>\n<body>\n'],mfile);
0269 
0270 %-------------------------------------------------------------------------------
0271 function xhtml_file_end(fid)
0272     fprintf(fid,'\n</body>\n</html>');
0273 
0274 %-------------------------------------------------------------------------------
0275 function format = xhtml_format
0276     format.string    = '<span class="string">%s</span>';
0277     format.comment   = '<span class="comment">%s</span>';
0278     format.code      = '%s'; %'<span class="code">%s</span>';
0279     format.keyword   = '<span class="keyword">%s</span>';
0280     format.cont      = '<span class="cont">%s</span>';
0281     format.pre_start = '<pre class="mcode">';
0282     format.pre_end   = '</pre>\n';
0283     format.nb_line   = '%04d ';
0284     format.line      = '%s\n';
0285 
0286 %-------------------------------------------------------------------------------
0287 function str = xhtml_special_char(str)
0288     %- See http://www.w3.org/TR/html4/charset.html#h-5.3.2
0289     str = strrep(str,'&','&amp;');
0290     str = strrep(str,'<','&lt;');
0291     str = strrep(str,'>','&gt;');
0292     str = strrep(str,'"','&quot;');
0293 
0294 
0295 %===============================================================================
0296 %                                   XML FORMAT                                 %
0297 %===============================================================================
0298 function xml_file_start(fid,mfile)
0299     fprintf(fid,[ ...
0300     '<?xml version="1.0" encoding="iso-8859-1" ?>\n' ...
0301     '<!DOCTYPE mfile SYSTEM "matlab.dtd">\n' ...
0302     '<mfile name="%s">\n'],mfile);
0303 
0304 %-------------------------------------------------------------------------------
0305 function xml_file_end(fid)
0306     fprintf(fid,'</mfile>');
0307 
0308 %-------------------------------------------------------------------------------
0309 function format = xml_format
0310     format.string    = '<string>%s</string>';
0311     format.comment   = '<comment>%s</comment>';
0312     format.code      = '%s'; %'<code>%s</code>';
0313     format.keyword   = '<keyword>%s</keyword>';
0314     format.cont      = '<cont>%s</cont>';
0315     format.pre_start = '';
0316     format.pre_end   = '';
0317     format.nb_line   = '<line nb="%04d ">';
0318     format.line      = '%s</line>\n';
0319 
0320 %-------------------------------------------------------------------------------
0321 function str = xml_special_char(str)
0322     %- See http://www.w3.org/TR/REC-xml#sec-predefined-ent
0323     str = strrep(str,'&','&amp;');
0324     str = strrep(str,'<','&lt;');
0325     str = strrep(str,'>','&gt;');
0326     str = strrep(str,'"','&quot;');
0327     %str = strrep(str,'''','&apos;');
0328     
0329 %===============================================================================
0330 %                                  LaTeX FORMAT                                %
0331 %===============================================================================
0332 function tex_file_start(fid,mfile)
0333     fprintf(fid,['\\documentclass[a4paper,10pt]{article}\n' ...
0334                  '    \\usepackage{alltt}\n' ...
0335                  '    \\usepackage{color}\n' ...
0336                  '    \\usepackage{fullpage}\n' ...
0337                  '    \\definecolor{string}{rgb}{0.7,0.0,0.0}\n' ...
0338                  '    \\definecolor{comment}{rgb}{0.13,0.54,0.13}\n' ...
0339                  '    \\definecolor{keyword}{rgb}{0.0,0.0,1.0}\n' ...
0340                  '    \\title{%s}\n' ...
0341                  '    \\author{\\textsc{Matlab}, The Mathworks, Inc.}\n' ...
0342                  '\\begin{document}\n' ...
0343                  '\\maketitle\n'],mfile);
0344 
0345 %-------------------------------------------------------------------------------
0346 function tex_file_end(fid)
0347     fprintf(fid,'\\end{document}');
0348 
0349 %-------------------------------------------------------------------------------
0350 function format = tex_format
0351     format.string    = '\\textcolor{string}{%s}';
0352     format.comment   = 'textcolor{comment}{%s}'; % '%' has been replaced by '\%'
0353     format.code      = '%s';
0354     format.keyword   = '\\textcolor{keyword}{%s}';
0355     format.cont      = '\\textcolor{keyword}{\\underline{%s}}';
0356     format.pre_start = '\\begin{alltt}\n';
0357     format.pre_end   = '\\end{alltt}\n';
0358     format.nb_line   = '%04d ';
0359     format.line      = '%s\n';
0360 
0361 %-------------------------------------------------------------------------------
0362 function str = tex_special_char(str)
0363     % Special characters: # $ % & ~ _ ^ \ { }
0364     str = strrep(str,'\','\(\backslash\)'); % $\backslash$ or \textbackslash or \verb+\+
0365     str = strrep(str,'#','\#');
0366     str = strrep(str,'$','\$');
0367     str = strrep(str,'%','\%');
0368     str = strrep(str,'&','\&');
0369     str = strrep(str,'_','\_');
0370     str = strrep(str,'{','\{');
0371     str = strrep(str,'}','\}');
0372     str = strrep(str,'^','\^{}');
0373     str = strrep(str,'~','\~{}'); % or \textasciitilde
0374 
0375 %===============================================================================
0376 %                                   RTF FORMAT                                 %
0377 %===============================================================================
0378 function rtf_file_start(fid,mfile)
0379     fprintf(fid,['{\\rtf1\\ansi\n\n' ...
0380                  '{\\fonttbl{\\f0\\fmodern\\fcharset0\\fprq1 Courier New;}}\n\n' ...
0381                  '{\\colortbl;\n' ...
0382                  '\\red0\\green0\\blue0;\n' ...
0383                  '\\red0\\green0\\blue255;\n' ...
0384                  '\\red33\\green138\\blue33;\n' ...
0385                  '\\red178\\green0\\blue0;}\n\n' ...
0386                  '{\\info{\\title %s}\n' ...
0387                  '{\\author HighLight.m Copyright 2003}\n' ...
0388                  '{\\creatim\\yr%s\\mo%s\\dy%s}' ...
0389                  '{\\*\\manager Guillaume Flandin}\n' ...
0390                  '{\\*\\company gllmflndn.com}\n' ...
0391                  '{\\*\\hlinkbase https://gllmflndn.com/software/' ... 
0392                  'matlab/highlight/}}\n\n'],mfile,...
0393                  datestr(date,'yyyy'),datestr(date,'mm'),datestr(date,'dd'));
0394 
0395 %-------------------------------------------------------------------------------
0396 function rtf_file_end(fid)
0397     fprintf(fid,'}');
0398 
0399 %-------------------------------------------------------------------------------
0400 function format = rtf_format
0401     format.string    = '{\\cf4 %s}';
0402     format.comment   = '{\\cf3 %s}';
0403     format.code      = '{%s}';
0404     format.keyword   = '{\\cf2 %s}';
0405     format.cont      = '{\\cf2 %s}';
0406     format.pre_start = '{\\f0\\fs16{';
0407     format.pre_end   = '}}';
0408     format.nb_line   = '{%04d }';
0409     format.line      = '%s\n\\par ';
0410 
0411 %-------------------------------------------------------------------------------
0412 function str = rtf_special_char(str)
0413     str = strrep(str,'\','\\');
0414     str = strrep(str,'{','\{');
0415     str = strrep(str,'}','\}');
0416 
0417 %===============================================================================
0418 %                                 MATLAB KEYWORDS                              %
0419 %===============================================================================
0420 function matlabKeywords = getMatlabKeywords
0421     %matlabKeywords = iskeyword; % Matlab R13
0422     matlabKeywords = {'break', 'case', 'catch', 'continue', 'elseif', 'else',...
0423                       'end', 'for', 'function', 'global', 'if', 'otherwise', ...
0424                       'persistent', 'return', 'switch', 'try', 'while'};
0425                       
0426 %-------------------------------------------------------------------------------
0427 function mKeySort = getMatlabKeywordsSorted
0428     mKeySort.nextincr = {'for', 'while', 'if', 'else', 'elseif', ...
0429                          'case', 'otherwise', 'try', 'catch'};
0430     mKeySort.nextincr2 = {'switch'};
0431     mKeySort.currdecr = {'else', 'elseif', 'case', 'otherwise', 'catch'};
0432     mKeySort.nextdecr = {'end'};
0433     mKeySort.other    = {'break', 'continue', 'function', 'global', ...
0434                          'persistent', 'return'};
0435 
0436 %===============================================================================
0437 %                               HANDLE TAB CHARACTER                           %
0438 %===============================================================================
0439 function str = horztab(str,n)
0440     %- For browsers, the horizontal tab character is the smallest non-zero 
0441     %- number of spaces necessary to line characters up along tab stops that are
0442     %- every 8 characters: behaviour obtained when n = 0.
0443     if n > 0
0444         str = strrep(str,sprintf('\t'),blanks(n));
0445     end
0446 
0447 %===============================================================================
0448 %                                MATLAB CODE PARSER                            %
0449 %===============================================================================
0450 function splitc = splitcode(code)
0451     %Split a line of  Matlab code in string, comment and other
0452 
0453     %- Label quotes in {'transpose', 'beginstring', 'midstring', 'endstring'}
0454     iquote = findstr(code,'''');
0455     quotetransp = [double('_''.)}]') ...
0456                    double('A'):double('Z') ...
0457                    double('0'):double('9') ...
0458                    double('a'):double('z')];
0459     flagstring = 0;
0460     flagdoublequote = 0;
0461     jquote = [];
0462     for i=1:length(iquote)
0463         if ~flagstring
0464             if iquote(i) > 1 & any(quotetransp == double(code(iquote(i)-1)))
0465                 % => 'transpose';
0466             else
0467                 % => 'beginstring';
0468                 jquote(size(jquote,1)+1,:) = [iquote(i) length(code)];
0469                 flagstring = 1;
0470             end
0471         else % if flagstring
0472             if flagdoublequote | ...
0473                (iquote(i) < length(code) & strcmp(code(iquote(i)+1),''''))
0474                 % => 'midstring';
0475                 flagdoublequote = ~flagdoublequote;
0476             else
0477                 % => 'endstring';
0478                 jquote(size(jquote,1),2) = iquote(i);
0479                 flagstring = 0;
0480             end
0481         end
0482     end
0483 
0484     %- Find if a portion of code is a comment
0485     ipercent = findstr(code,'%');
0486     jpercent = [];
0487     for i=1:length(ipercent)
0488         if isempty(jquote) | ...
0489            ~any((ipercent(i) > jquote(:,1)) & (ipercent(i) < jquote(:,2)))
0490             jpercent = [ipercent(i) length(code)];
0491             break;
0492         end
0493     end
0494 
0495     %- Find continuation punctuation '...'
0496     icont = findstr(code,'...');
0497     for i=1:length(icont)
0498         if (isempty(jquote) | ...
0499             ~any((icont(i) > jquote(:,1)) & (icont(i) < jquote(:,2)))) & ...
0500             (isempty(jpercent) | ...
0501             icont(i) < jpercent(1))
0502             jpercent = [icont(i) length(code)];
0503             break;
0504         end
0505     end
0506 
0507     %- Remove strings inside comments
0508     if ~isempty(jpercent) & ~isempty(jquote)
0509         jquote(find(jquote(:,1) > jpercent(1)),:) = [];
0510     end
0511 
0512     %- Split code in a cell array of strings
0513     icode = [jquote ; jpercent];
0514     splitc = {};
0515     if isempty(icode)
0516         splitc{1} = code;
0517     elseif icode(1,1) > 1
0518         splitc{1} = code(1:icode(1,1)-1);
0519     end
0520     for i=1:size(icode,1)
0521         splitc{end+1} = code(icode(i,1):icode(i,2));
0522         if i < size(icode,1) & icode(i+1,1) > icode(i,2) + 1
0523             splitc{end+1} = code((icode(i,2)+1):(icode(i+1,1)-1));
0524         elseif i == size(icode,1) & icode(i,2) < length(code)
0525             splitc{end+1} = code(icode(i,2)+1:end);
0526         end
0527     end
0528       
0529 %===============================================================================
0530 %                           MODIFIED VERSION OF STRTOK                         %
0531 %===============================================================================
0532 function [token, remainder, quotient] = strtok(string, delimiters)
0533 % Modified version of STRTOK to also return the quotient
0534 % string = [quotient token remainder]
0535 %STRTOK Find token in string.
0536 %   STRTOK(S) returns the first token in the string S delimited
0537 %   by "white space".   Any leading white space characters are ignored.
0538 %
0539 %   STRTOK(S,D) returns the first token delimited by one of the 
0540 %   characters in D.  Any leading delimiter characters are ignored.
0541 %
0542 %   [T,R] = STRTOK(...) also returns the remainder of the original
0543 %   string.
0544 %   If the token is not found in S then R is an empty string and T
0545 %   is same as S. 
0546 %
0547 %   Copyright 1984-2002 The MathWorks, Inc. 
0548 %   $Revision: 5.14 $  $Date: 2002/04/09 00:33:38 $
0549 
0550 token = []; remainder = []; quotient = string;
0551 
0552 len = length(string);
0553 if len == 0
0554     return
0555 end
0556 
0557 if (nargin == 1)
0558     delimiters = [9:13 32]; % White space characters
0559 end
0560 
0561 i = 1;
0562 while (any(string(i) == delimiters))
0563     i = i + 1;
0564     if (i > len), return, end
0565 end
0566 start = i;
0567 while (~any(string(i) == delimiters))
0568     i = i + 1;
0569     if (i > len), break, end
0570 end
0571 finish = i - 1;
0572 
0573 token = string(start:finish);
0574 
0575 if (nargout >= 2)
0576     remainder = string(finish + 1:length(string));
0577 end
0578 
0579 if (nargout == 3 & start > 1)
0580     quotient = string(1:start-1);
0581 else
0582     quotient = [];
0583 end