\font\WebGfnt=dcr1000
\def\WebLG{\hbox{\WebGfnt\char14}}  % left guillemet (french quote)
\def\WebRG{\hbox{\WebGfnt\char15}}  % right guillemet
\def\WebGkern{-.2em\relax}          % kern between guillemets
\def\WebAkern{-.05em\relax}         % kern after

\def\title{A test program for MWEB (Matlab Web)}
\def\topofcontents{\centerline{\titlefont\title}
  \vfill
  \centerline{Mark Potse}
  \vfill} 

@* Example.  This file serves to test {\tt MWEB} (Matlab Web).
@d add(a,b) = ((a) + (b))
@f alpha TeX
@f beta TeX
@f phi tex

@u
@1
@<used code@>

  phi = alpha/beta;
  c = add  (1 ,   'ee');
a	= "b";
switch a
  case "b", 	a=1;	%normal comment
  case "c", a=3; 		%normal comment with a space and a tab
  case "d", a=5; @% controlled comment with a space
end@% controlled comment without a space
clear a b c %foo

set(gcf,'Color', get(gcf,'Color'));
x, y, alpha b c(foo);

foo = (a  ...
   + b - c);

str = '\';
str = '/';


@*=Formatted strings. Particularly when creating a user interface,
Matlab programmers may want to write nested strings containing code up
to three or four levels. This example program defines a
WindowButtonDownFcn that in turn defines a WindowButtonMotionFcn and a
WindowButtonUpFcn, which in turn undefines the
WindowButtonMotionFcn. Some parts of the code are put in refinements,
to keep this section comprehensible.

@<used code@>= 
   set(gcf,'WindowButtonDownFcn', `
      @<create the line@>
      set(gcf,"WindowButtonMotionFcn", `@<reset the line@>');
      set(gcf,"WindowButtonUpFcn", `
          fix_line;
	  set(gcf, "WindowButtonMotionFcn",` ');');
    ');

  At = A';
  str3 = `str = `do_transpose=`At = A"; '; '; ';
  str4 = ``````foo'''''';

@ The WindowButtonDownFcn gets the mouse position and creates a line,
whose begin- and endpoints are the same.

@<create the line@>=
lb = get(gca,"CurrentPoint");
  le=lb;
  L=line([lb(1,1),le(1,1)], [lb(1,2), le(1,2)], ...
   "EraseMode", "xor");


@ Then, the WindowButtonMotionFcn makes the endpoint of the line move
with the mouse pointer.

@<reset the line@>=
le = get(gca,"CurrentPoint");
set(L,"XData",[lb(1,1),le(1,1)], ...
    "YData", [lb(1,2), le(1,2)]);


@ And at last, the WindowButtonUpFcn changes the colour of the line
and, by undefining the WindowButtonMotionFcn, fixes it.

@d fix_line = eval(`set(L, ...
      "Color", "r");');



@* More examples. The following code is just an example of several
Matlab constructs; it does nothing useful.

@
@<unused code@>=
  a = 2;
  b = a + 5*12.4 + 2/3^2 - 21.7 & 5;
  @<some refinement@>
  str = ['This  string  ', '        contains ''lots''     of spaces'];
  @<some refinement@>
  flarp = rand(3,8) ./ linspace(3,8);
  @<some refinement@>


@ Some more examples of formatted strings (initialized with a left
quote), with a macro or a module inside.

@d incr(f) = f=1+(f)
@<unused code@>=


  str1 = `foo=bar; fun(foo);
     fun(boo); 
     incr(foo);'
  str2 = `foo=bar; fun(foo);
     fun(boo); 
     @<some refinement@>'
  At = A';
  str3 = `str = `do_transpose=`At = A"; '; '; ';
  str4 = ``````foo'''''';

@ Array elements may be separated by commas or spaces or both.
@<unused code@>=
  x = [0 0 -2 pi,2*2, 2*pi,5, -4.5 20 9243857 inf];
  a = 1/4;
  aa = 1.25e4 / 3.54E10;
  b = 2e20i,  c = pi/4;


@ if-elseif-else-end constructs:
@<unused code@>=

   if (a ~= b),  a = b+1;  end
   if (a ~= b)  a = b+1;  end
   if a==b
      a = b;
   end; 
   if a==3 a = b; elseif b==4, b = a; else a = c;  end; 

   if (a ~= b) & a < c | ...
     (c >= 30) xor a < 3
     @<some refinement@> @;
   else if a==25
           c = 4.67/2+8*6^3;
        elseif a<b
           c = c+3;
        else
           disp('foo');
        end
   end


@
@<some refinement@>=

   if (a == b)
   a_variable_with_a_supercalifragilisticxpalidociously_long_name_that_louses_up_the_identifier_index = 2;
     @% this is a controlled comment, introduced with @@%
     @% text(2, 3, 'blah bla blah');
   elseif ( a< b)
     foo = 45;
   elseif x
     if x == 1
	y = x;
     elseif (x == 2)
	y = x + 1;
     else
	y = x + 3;
     end
   else
     text(-2, 3, 'blah bla blah');
     text(10, -7, 'bla blah blah');
   end


@ 
@<unused code@>=

   for i = 1:10
      j(i) = pi+i;
   end
   for i = [1:10],   j(i) = pi+i;   end


@ A pathological |for| statement:
@.``at-period'' index entry@>
@^``at-hat'' index entry@>
@<unused code@>=
   for i = 1:10
      j(i) = pi+i;
   end
   for i = 1:10
      for j = 1,5,6
      for k = 1, 3, 4, 23, 24, 83,34,34,23,25,25,27, 56 ,25, 45,3, 5, ...
   	      9, 3, 6, 234, 56,  32324, pi, 432, 234, 453, 345, 345,93, 93,... 
   	      83, i, j, 8, 9, 10, 54, 843, 845, 8342, 7234, 7834
   	  matrix = zeros(21,20);
   	  vect(i) = matrix(2*i+1,j);
      end
      if a==b
         a = a + 2;
      end
      end
   end

@ Switch/case is new in Matlab version 5. 
@<unused code@>=
switch a_variable
  case 0
      c = 3;
      b = a+(c^2-24)/pi;
  case 1,  a = 2;
  case {2,3},  a = 3;
  otherwise a = 4+5;
end
b = a+1;

@ another pathological |case|.
@<unused code@>=
switch a_variable + a_variable_with_a_long_name - 23239847 + 1.24e6/34 - ...
       1 + a+b^2+4c
  case 0
      c = 3;
      b = a+(c^2-24)/pi;
  case 1,  a = 2;
  case 2,  a = 3;
  otherwise
    a = 4+5;
end
b = a+1;

if a_variable + a_variable_with_a_long_name - 23239847 + 1.24e6/34 ...
       - 1 + a+b^2+4*c,
  b = c;
end;

if a_variable + a_variable_with_a_long_name - 23239847 + 1.24e6/34 ...
       - 1 + a+b^2+4*c
  b = c;
end;


@ This is just to fill the index.
@.``at-period'' index entry@>
@^``at-hat'' index entry@>
@^another index entry@>
@^zz index entry@>
@<unused code@>= a, b, c, d, e, f, g, h

i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;

za, zb, zc, zd,
ae, af, zg, zh, zi, zj, zk, zl, zm, zn, zo, zp, zq, zr, zs, zt, zu
av, zw, x, zy, zz;

zzz_;
zzz;
ZZa
ZZZ

@* Index.





