KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cprintf.m
Go to the documentation of this file.
1 
2 
3 /* (Autoinserted by mtoc++)
4  * This source code has been filtered by the mtoc++ executable,
5  * which generates code that can be processed by the doxygen documentation tool.
6  *
7  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
8  * Except for the comments, the function bodies of your M-file functions are untouched.
9  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
10  * attached source files that are highly readable by humans.
11  *
12  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
13  * the correct locations in the source code browser.
14  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
15  */
16 
17 function count = cprintf(style,format,varargin) {
18 
19 
20 /* License to use and modify this code is granted freely to all interested, as long as the original author is
21  * referenced and attributed as such. The original author maintains the right to be solely associated with this work. */
22 
23 /* Programmed and Copyright by Yair M. Altman: altmany(at)gmail.com
24  * $Revision: 1.08 $ $Date: 2012/10/17 21:41:09 $ */
25 
26  persistent majorVersion minorVersion
27  if isempty(majorVersion)
28  /* v = version; if str2double(v(1:3)) <= 7.1
29  *majorVersion = str2double(regexprep(version,'^(\d+).*','$1'));
30  *minorVersion = str2double(regexprep(version,'^\d+\.(\d+).*','$1'));
31  *[a,b,c,d,versionIdStrs]=regexp(version,'^(\d+)\.(\d+).*'); %#ok unused */
32  v = sscanf(version, " %d. ", 2);
33  majorVersion = v(1); /* str2double(versionIdStrs{1}{1}); */
34 
35  minorVersion = v(2); /* str2double(versionIdStrs{1}{2}); */
36 
37  end
38 
39  /* The following is for debug use only:
40  *global docElement txt el */
41  if ~exist(" el "," var ") || isempty(el), el=handle([]); end /* #ok mlint short-circuit error ("used before defined") */
42 
43  if nargin<1, showDemo(majorVersion,minorVersion); return; end
44  if isempty(style), return; end
45  if all(ishandle(style)) && length(style)~=3
46  dumpElement(style);
47  return;
48  end
49 
50  /* Process the text string */
51  if nargin<2, format = style; style=" text "; end
52  /* error(nargchk(2, inf, nargin, 'struct'));
53  *str = sprintf(format,varargin{:}); */
54 
55  /* In compiled mode */
56  try useDesktop = usejava(" desktop "); catch, useDesktop = false; end
57  if isdeployed | ~useDesktop /* #ok<OR2> - for Matlab 6 compatibility
58  * do not display any formatting - use simple fprintf()
59  * See: http://undocumentedmatlab.com/blog/bold-color-text-in-the-command-window/#comment-103035
60  * Also see: https://mail.google.com/mail/u/0/?ui=2&shva=1#all/1390a26e7ef4aa4d
61  * Also see: https://mail.google.com/mail/u/0/?ui=2&shva=1#all/13a6ed3223333b21 */
62 
63  count1 = fprintf(format,varargin[:]);
64  else
65  /* Else (Matlab desktop mode)
66  * Get the normalized style name and underlining flag */
67  [underlineFlag, boldFlag, style] = processStyleInfo(style);
68 
69  /* Set hyperlinking, if so requested */
70  if underlineFlag
71  format = [" <a href=''> " format " </a> "];
72 
73  /* Matlab 7.1 R14 (possibly a few newer versions as well?)
74  * have a bug in rendering consecutive hyperlinks
75  * This is fixed by appending a single non-linked space */
76  if majorVersion < 7 || (majorVersion==7 && minorVersion <= 1)
77  format(end+1) = " ";
78  end
79  end
80 
81  /* Set bold, if requested and supported (R2011b+) */
82  if boldFlag
83  if (majorVersion > 7 || minorVersion >= 13)
84  format = [" <strong> " format " </strong> "];
85  else
86  boldFlag = 0;
87  end
88  end
89 
90  /* Get the current CW position */
91  cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
92  lastPos = cmdWinDoc.getLength;
93 
94  /* If not beginning of line */
95  bolFlag = 0; /* #ok
96  *if docElement.getEndOffset - docElement.getStartOffset > 1
97  * Display a hyperlink element in order to force element separation
98  * (otherwise adjacent elements on the same line will be merged) */
99 
100  if majorVersion<7 || (majorVersion==7 && minorVersion<13)
101  if ~underlineFlag
102  fprintf(" <a href=''> </a> "); /* fprintf('<a href=""> </a>\b'); */
103 
104  elseif format(end)~=10 /* if no newline at end */
105 
106  fprintf(" "); /* fprintf(' \b'); */
107 
108  end
109  end
110  /* drawnow; */
111  bolFlag = 1;
112  /* end */
113 
114  /* Get a handle to the Command Window component */
115  mde = com.mathworks.mde.desk.MLDesktop.getInstance;
116  cw = mde.getClient(" Command Window ");
117  xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
118 
119  /* Store the CW background color as a special color pref
120  * This way, if the CW bg color changes (via File/Preferences),
121  * it will also affect existing rendered strs */
122  com.mathworks.services.Prefs.setColorPref(" CW_BG_Color ",xCmdWndView.getBackground);
123 
124  /* Display the text in the Command Window */
125  count1 = fprintf(2,format,varargin[:]);
126 
127  /* awtinvoke(cmdWinDoc,'remove',lastPos,1); % TODO: find out how to remove the extra '_' */
128  drawnow; /* this is necessary for the following to work properly (refer to Evgeny Pr in FEX comment 16/1/2011) */
129 
130  docElement = cmdWinDoc.getParagraphElement(lastPos+1);
131  if majorVersion<7 || (majorVersion==7 && minorVersion<13)
132  if bolFlag && ~underlineFlag
133  /* Set the leading hyperlink space character ('_') to the bg color, effectively hiding it
134  * Note: old Matlab versions have a bug in hyperlinks that need to be accounted for...
135  *disp(' '); dumpElement(docElement) */
136  setElementStyle(docElement," CW_BG_Color ",1+underlineFlag,majorVersion,minorVersion); /* +getUrlsFix(docElement));
137  *disp(' '); dumpElement(docElement) */
138 
139  el(end+1) = handle(docElement); /* #ok used in debug only */
140 
141  end
142 
143  /* Fix a problem with some hidden hyperlinks becoming unhidden... */
144  fixHyperlink(docElement);
145  /* dumpElement(docElement); */
146  end
147 
148  /* Get the Document Element(s) corresponding to the latest fprintf operation */
149  while docElement.getStartOffset < cmdWinDoc.getLength
150  /* Set the element style according to the current style
151  *disp(' '); dumpElement(docElement) */
152  specialFlag = underlineFlag | boldFlag;
153  setElementStyle(docElement,style,specialFlag,majorVersion,minorVersion);
154  /* disp(' '); dumpElement(docElement) */
155  docElement2 = cmdWinDoc.getParagraphElement(docElement.getEndOffset+1);
156  if isequal(docElement,docElement2), break; end
157  docElement = docElement2;
158  /* disp(' '); dumpElement(docElement) */
159  end
160 
161  /* Force a Command-Window repaint
162  * Note: this is important in case the rendered str was not '\n'-terminated */
163  xCmdWndView.repaint;
164 
165  /* The following is for debug use only: */
166  el(end+1) = handle(docElement); /* #ok used in debug only
167  *elementStart = docElement.getStartOffset;
168  *elementLength = docElement.getEndOffset - elementStart;
169  *txt = cmdWinDoc.getText(elementStart,elementLength); */
170 
171  end
172 
173  if nargout
174  count = count1;
175  end
176  return; /* debug breakpoint */
177 
178 
179 /* Process the requested style information */
180 }
298 function [underlineFlag , boldFlag , style ] = cprintf>processStyleInfo(style) {
299  underlineFlag = 0;
300  boldFlag = 0;
301 
302  /* First, strip out the underline/bold markers */
303  if ischar(style)
304  /* Styles containing '-' or '_' should be underlined (using a no-target hyperlink hack)
305  *if style(1)=='-' */
306  underlineIdx = (style==" - ") | (style==" _ ");
307  if any(underlineIdx)
308  underlineFlag = 1;
309  /* style = style(2:end); */
310  style = style(~underlineIdx);
311  end
312 
313  /* Check for bold style (only if not underlined) */
314  boldIdx = (style==" * ");
315  if any(boldIdx)
316  boldFlag = 1;
317  style = style(~boldIdx);
318  end
319  if underlineFlag && boldFlag
320  warning(" YMA:cprintf:BoldUnderline "," Matlab does not support both bold & underline ")
321  end
322 
323  /* Check if the remaining style sting is a numeric vector
324  *styleNum = str2num(style); %#ok<ST2NM> % not good because style='text' is evaled!
325  *if ~isempty(styleNum) */
326  if any(style==" " | style==" , " | style==" ; ")
327  style = str2num(style); /* #ok<ST2NM> */
328 
329  end
330  end
331 
332  /* Style = valid matlab RGB vector */
333  if isnumeric(style) && length(style)==3 && all(style<=1) && all(abs(style)>=0)
334  if any(style<0)
335  underlineFlag = 1;
336  style = abs(style);
337  end
338  style = getColorStyle(style);
339 
340  elseif ~ischar(style)
341  error(" YMA:cprintf:InvalidStyle "," Invalid style - see help section for a list of valid style values ")
342 
343  /* Style name */
344  else
345  /* Try case-insensitive partial/full match with the accepted style names */
346  validStyles = [" Text "," Keywords "," Comments "," Strings "," UnterminatedStrings "," SystemCommands "," Errors ", ...
347  " Black "," Cyan "," Magenta "," Blue "," Green "," Red "," Yellow "," White ", ...
348  " Hyperlinks "];
349  matches = find(strncmpi(style,validStyles,length(style)));
350 
351  /* No match - error */
352  if isempty(matches)
353  error(" YMA:cprintf:InvalidStyle "," Invalid style - see help section for a list of valid style values ")
354 
355  /* Too many matches (ambiguous) - error */
356  elseif length(matches) > 1
357  error(" YMA:cprintf:AmbigStyle "," Ambiguous style name - supply extra characters for uniqueness ")
358 
359  /* Regular text */
360  elseif matches == 1
361  style = " ColorsText "; /* fixed by Danilo, 29/8/2011 */
362 
363 
364  /* Highlight preference style name */
365  elseif matches < 8
366  style = [" Colors_M_ " validStyles[matches]];
367 
368  /* Color name */
369  elseif matches < length(validStyles)
370  colors = [0,0,0; 0,1,1; 1,0,1; 0,0,1; 0,1,0; 1,0,0; 1,1,0; 1,1,1];
371  requestedColor = colors(matches-7,:);
372  style = getColorStyle(requestedColor);
373 
374  /* Hyperlink */
375  else
376  style = " Colors_HTML_HTMLLinks "; /* CWLink */
377 
378  underlineFlag = 1;
379  end
380  end
381 
382 /* Convert a Matlab RGB vector into a known style name (e.g., '[255,37,0]') */
383 }
384 
385 function styleName = cprintf>getColorStyle(rgb) {
386  intColor = int32(rgb*255);
387  javaColor = java.awt.Color(intColor(1), intColor(2), intColor(3));
388  styleName = sprintf(" [%d,%d,%d] ",intColor);
389  com.mathworks.services.Prefs.setColorPref(styleName,javaColor);
390 
391 /* Fix a bug in some Matlab versions, where the number of URL segments
392  * is larger than the number of style segments in a doc element */
393 }
394 
395 function delta = cprintf>getUrlsFix(docElement) {
396  tokens = docElement.getAttribute(" SyntaxTokens ");
397  links = docElement.getAttribute(" LinkStartTokens ");
398  if length(links) > length(tokens(1))
399  delta = length(links) > length(tokens(1));
400  else
401  delta = 0;
402  end
403 
404 /* fprintf(2,str) causes all previous '_'s in the line to become red - fix this */
405 }
406 
407 function cprintf>fixHyperlink(docElement) {
408  try
409  tokens = docElement.getAttribute(" SyntaxTokens ");
410  urls = docElement.getAttribute(" HtmlLink ");
411  urls = urls(2);
412  links = docElement.getAttribute(" LinkStartTokens ");
413  offsets = tokens(1);
414  styles = tokens(2);
415  doc = docElement.getDocument;
416 
417  /* Loop over all segments in this docElement */
418  for idx = 1 : length(offsets)-1
419  /* If this is a hyperlink with no URL target and starts with ' ' and is collored as an error (red)... */
420  if strcmp(styles(idx).char," Colors_M_Errors ")
421  character = char(doc.getText(offsets(idx)+docElement.getStartOffset,1));
422  if strcmp(character," ")
423  if isempty(urls(idx)) && links(idx)==0
424  /* Revert the style color to the CW background color (i.e., hide it!) */
425  styles(idx) = java.lang.String(" CW_BG_Color ");
426  end
427  end
428  end
429  end
430  catch
431  /* never mind... */
432  end
433 
434 /* Set an element to a particular style (color) */
435 }
436 
437 function cprintf>setElementStyle(docElement,style,specialFlag,majorVersion,minorVersion) {
438  global oldStyles
439  if nargin<3, specialFlag=0; end
440  /* Set the last Element token to the requested style:
441  * Colors: */
442  tokens = docElement.getAttribute(" SyntaxTokens ");
443  try
444  styles = tokens(2);
445  oldStyles[end+1] = styles.cell;
446 
447  /* Correct edge case problem */
448  extraInd = double(majorVersion>7 || (majorVersion==7 && minorVersion>=13)); /* =0 for R2011a-, =1 for R2011b+
449  *{ */
450 
451  if ~strcmp(" CWLink ",char(styles(end-hyperlinkFlag))) && ...
452  strcmp(" CWLink ",char(styles(end-hyperlinkFlag-1)))
453  extraInd = 0;/* 1; */
454 
455  end
456  hyperlinkFlag = ~isempty(strmatch(" CWLink ",tokens(2)));
457  hyperlinkFlag = 0 + any(cellfun(@(c)(~isempty(c)&&strcmp(c," CWLink ")),tokens(2).cell));
458  /* } */
459 
460  styles(end-extraInd) = java.lang.String();
461  styles(end-extraInd-specialFlag) = java.lang.String(style); /* #ok apparently unused but in reality used by Java */
462 
463  if extraInd
464  styles(end-specialFlag) = java.lang.String(style);
465  end
466 
467  oldStyles[end] = [oldStyles[end] styles.cell];
468  catch
469  /* never mind for now */
470  end
471 
472  /* Underlines (hyperlinks):
473  *{ */
474  links = docElement.getAttribute(" LinkStartTokens ");
475  if isempty(links)
476  /* docElement.addAttribute('LinkStartTokens',repmat(int32(-1),length(tokens(2)),1)); */
477  else
478  /* TODO: remove hyperlink by setting the value to -1 */
479  end
480  /* } */
481 
482  /* Correct empty URLs to be un-hyperlinkable (only underlined) */
483  urls = docElement.getAttribute(" HtmlLink ");
484  if ~isempty(urls)
485  urlTargets = urls(2);
486  for urlIdx = 1 : length(urlTargets)
487  try
488  if urlTargets(urlIdx).length < 1
489  urlTargets(urlIdx) = []; /* '' => [] */
490 
491  end
492  catch
493  /* never mind... */
494  a=1; /* #ok used for debug breakpoint... */
495 
496  end
497  end
498  end
499 
500  /* Bold: (currently unused because we cannot modify this immutable int32 numeric array)
501  *{ */
502  try
503  /* hasBold = docElement.isDefined('BoldStartTokens'); */
504  bolds = docElement.getAttribute(" BoldStartTokens ");
505  if ~isempty(bolds)
506  /* docElement.addAttribute('BoldStartTokens',repmat(int32(1),length(bolds),1)); */
507  end
508  catch
509  /* never mind - ignore... */
510  a=1; /* #ok used for debug breakpoint... */
511 
512  end
513  /* } */
514 
515  return; /* debug breakpoint */
516 
517 
518 /* Display information about element(s) */
519 }
520 
521 function cprintf>dumpElement(docElements) {
522  numElements = length(docElements);
523  cmdWinDoc = docElements(1).getDocument;
524  for elementIdx = 1 : numElements
525  if numElements > 1, fprintf(" Element #%d:\n ",elementIdx); end
526  docElement = docElements(elementIdx);
527  if ~isjava(docElement), docElement = docElement.java; end
528  /* docElement.dump(java.lang.System.out,1) */
529  disp(" ");
530  disp(docElement)
531  tokens = docElement.getAttribute(" SyntaxTokens ");
532  if isempty(tokens), continue; end
533  links = docElement.getAttribute(" LinkStartTokens ");
534  urls = docElement.getAttribute(" HtmlLink ");
535  try bolds = docElement.getAttribute(" BoldStartTokens "); catch, bolds = []; end
536  txt = [];
537  tokenLengths = tokens(1);
538  for tokenIdx = 1 : length(tokenLengths)-1
539  tokenLength = diff(tokenLengths(tokenIdx+[0,1]));
540  if (tokenLength < 0)
541  tokenLength = docElement.getEndOffset - docElement.getStartOffset - tokenLengths(tokenIdx);
542  end
543  txt[tokenIdx] = cmdWinDoc.getText(docElement.getStartOffset+tokenLengths(tokenIdx),tokenLength).char; /* #ok */
544 
545  end
546  lastTokenStartOffset = docElement.getStartOffset + tokenLengths(end);
547  txt[end+1] = cmdWinDoc.getText(lastTokenStartOffset, docElement.getEndOffset-lastTokenStartOffset).char; /* #ok
548  *cmdWinDoc.uiinspect
549  *docElement.uiinspect */
550 
551  txt = strrep(txt" ,sprintf( "\n" ), "\n^t);
552  try
553  data = [tokens(2).cell m2c(tokens(1)) m2c(links) m2c(urls(1)) cell(urls(2)) m2c(bolds) txt];
554  if elementIdx==1
555  disp(" SyntaxTokens(2,1) - LinkStartTokens - HtmlLink(1,2) - BoldStartTokens - txt ");
556  disp(" ============================================================================== ");
557  end
558  catch
559  try
560  data = [tokens(2).cell m2c(tokens(1)) m2c(links) txt];
561  catch
562  disp([tokens(2).cell m2c(tokens(1)) txt]);
563  try
564  data = [m2c(links) m2c(urls(1)) cell(urls(2))];
565  catch
566  /* Mtlab 7.1 only has urls(1)... */
567  data = [m2c(links) urls.cell];
568  end
569  end
570  end
571  disp(data)
572  end
573 
574 /* Utility function to convert matrix => cell */
575 }
576 
577 function cells = cprintf>m2c(data) {
578  cells = num2cell(data);
579 
580 /* Display the help and demo */
581 }
582 
583 function cprintf>showDemo(majorVersion,minorVersion) {
584  fprintf(" cprintf displays formatted text in the Command Window.\n\n ");
585  fprintf(" Syntax: count = cprintf(style,format,...); click <a href='matlab:help cprintf'>here</a> for details.\n\n ");
586  url = " http://UndocumentedMatlab.com/blog/cprintf/ ";
587  fprintf([" Technical description: <a href=' " url " '> " url " </a>\n\n "]);
588  fprintf(" Demo:\n\n ");
589  boldFlag = majorVersion>7 || (majorVersion==7 && minorVersion>=13);
590  s = [" cprintf( "" text "" , "" regular black text "" ); " 10 ...
591  " cprintf( "" hyper "" , "" followed %s "" , "" by "" ); " 10 ...
592  " cprintf( "" key "" , "" %d colored "" , " num2str(4+boldFlag) " ); " 10 ...
593  " cprintf( "" -comment "" , "" & underlined "" ); " 10 ...
594  " cprintf( "" err "" , "" elements:\n "" ); " 10 ...
595  " cprintf( "" cyan "" , "" cyan "" ); " 10 ...
596  " cprintf( "" _green "" , "" underlined green "" ); " 10 ...
597  " cprintf(-[1,0,1], "" underlined magenta "" ); " 10 ...
598  " cprintf([1,0.5,0], "" and multi-\nline orange\n "" ); " 10];
599  if boldFlag
600  /* In R2011b+ the internal bug that causes the need for an extra space
601  * is apparently fixed, so we must insert the sparator spaces manually...
602  * On the other hand, 2011b enables *bold* format */
603  s = [s " cprintf( "" *blue "" , "" and *bold* (R2011b+ only)\n "" ); " 10];
604  s = strrep(s, " ) "," "" ) ");
605  s = strrep(s, " ,5) "," "" ,5) ");
606  s = strrep(s, " \n "," \n ");
607  end
608  disp(s);
609  eval(s);
610 }
611 
function cprintf>setElementStyle(docElement, style, specialFlag, majorVersion, minorVersion)
Definition: cprintf.m:437
function delta = cprintf>getUrlsFix(docElement)
Definition: cprintf.m:395
A MatLab cell array or matrix.
A double value.
function cprintf>showDemo(majorVersion, minorVersion)
Definition: cprintf.m:583
Matlab's base handle class (documentation generation substitute)
function cprintf>fixHyperlink(docElement)
Definition: cprintf.m:407
A variable number of input arguments.
function [ underlineFlag , boldFlag , style ] = cprintf>processStyleInfo(style)
Definition: cprintf.m:298
Speed test * all(1:3)
function cells = cprintf>m2c(data)
Definition: cprintf.m:577
function count = cprintf(style, format, varargin)
CPRINTF displays styled formatted text in the Command Window.
Definition: cprintf.m:17
function styleName = cprintf>getColorStyle(rgb)
Definition: cprintf.m:385
function cprintf>dumpElement(docElements)
Definition: cprintf.m:521