%
% $Id: example.web,v 1.9 2000/05/22 13:38:58 potse Exp $
%
%
% The following 5 lines of TeX code define how quotes of `formatted
% strings' look. They will only work if you have the dcr1000 font.
% You may change them, or remove them/comment them out, to get the
% default defined in mweb.tex, which should always work.
%
\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
%
% macros used here
%
\def\phin{\phi_{\rm in}}
%
% work with TeX/LaTeX:
%
\ifx\section\undefined
% this is for TeX
\def\title{An example program for MWEB (Matlab Web)}
\def\texttt#1{{\tt#1}}
\def\textit#1{{\it#1}}
\def\LaTeX{L\kern-.36em\raise.3ex\hbox{\sc
a}\kern-.15emT\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX}
\else
% this is for LaTeX
\def\xwebContentsTop{\centerline{Example}}
\fi
\chardef\other=12
\def\ttverbatim{\begingroup
\catcode`\\=\other
\catcode`\{=\other
\catcode`\}=\other
\catcode`\$=\other
\catcode`\&=\other
\catcode`\#=\other
\catcode`\%=\other
\catcode`\~=\other
\catcode`\_=\other
\catcode`\^=\other
\obeyspaces \obeylines \tt}
\outer\def\begintt{\let\par=\endgraf \ttverbatim \parskip=0pt
\catcode`\&=0 \rightskip-5pc \ttfinish}
{\catcode`\&=0 &catcode`&\=\other % & is temporary escape character
&obeylines % end of line is active
&gdef&ttfinish#1^^M#2\endtt{#1&vbox{#2}&endgroup}}
\catcode`\&=\active
{\obeylines \gdef&{\ttverbatim \spaceskip\ttglue \let^^M=\ \let&=\endgroup}}
@*2 Example. This file serves to test {\tt MWEB} (Matlab Web) and to
illustrate its features.
@ This code is written in a file called \texttt{lines.m}. This
function creates a figure window where lines can be drawn
interactively by pushing the left mouse button at the desired begin
point, dragging the mouse, and releasing the button at the desired end
point.
@(lines.m@>=
figure;
axes(...
'Units','normal', ...
'Position', [0,0,1,1], ...
'Visible','off', ...
'XLim', [0,1], ...
'YLim', [0,1], ...
'XLimMode','manual','YLimMode','manual');
@
@*2 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, which 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.
@=
set(gcf,'WindowButtonDownFcn', `
@
set(gcf,"WindowButtonMotionFcn", `@');
set(gcf,"WindowButtonUpFcn", `
fix_line;
set(gcf, "WindowButtonMotionFcn",` ');');
');
@ By the way, \texttt{mtangle} expands the above code into:
\medskip\begintt
set(gcf,'WindowButtonDownFcn',['',...
'lb = get(gca,''CurrentPoint'');',...
'le = lb;',...
'L = line([lb(1,1),le(1,1)],[lb(1,2),le(1,2)], ',...
'''EraseMode'',''xor'');',...
'set(gcf,''WindowButtonMotionFcn'',''le = get(gca,''''CurrentPoint'''');',...
'set(L,''''XData'''',[lb(1,1),le(1,1)],',...
'''''YData'''',[lb(1,2),le(1,2)]);',...
''');',...
'set(gcf,''WindowButtonUpFcn'',''',...
'set(L,''''Color'''',''''r'''');',...
'set(gcf,''''WindowButtonMotionFcn'''','''''''');'');',...
'']);
\endtt
@ The WindowButtonDownFcn determines the mouse position and creates a
line, whose begin- and endpoints are the same.
@=
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.
@=
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 = set(L, "Color", "r");
@*2 More examples. The following code is just an example of several
Matlab constructs; it does nothing useful.
@u
@
@
a_variable_ @& with_ @& a_com @& pound_name = 8;
@ Complex constants in Matlab are made by appending an `i'
character. In exponential notation, it follows the exponent.
@=
a = 2i;
z = 2e4 + 3e4i;
flarp = rand(3,8) ./ linspace(3,8);
@
@ Here, a macro argument is used in a formatted string that is part of
the replacement text of the macro. (This isn't useful, it's just an
example.)
@d print_arg(win,fun,arg) = set(win,"DeleteFcn", `fun(arg);')
@=
print_arg(gcf, disp, "aargh!");
print_arg(gca, fprintf, "foo!");
@ Array elements may be separated by commas or spaces or both.
@=
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:
@=
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 a==4, a = b/2; else a = a+1; end
if (a ~= b) & a < c | ...
(c >= 30) xor a < 3
@ @;
else if a==25
c = 4.67/2+8*6^3;
elseif a**
@^``at-hat'' index entry@>
@=
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
@*2 Custom-formatting of identifiers. In the limbo section of this file,
the following definition was made:
\medskip\begintt
\def\phin{\phi_{\rm in}}
\endtt
\medskip\noindent Which prints as ``|phin|.'' Then, the variable that
is typed as ``\texttt{phin}'' in the program prints as |phin| too, if
the following format statement is used:
@f phin TeX
@ We also want the identifier \textit{beta} to print as
|beta|. Because this definition is already provided by plain \TeX\ and
\LaTeX, we need not give it here, we just have to use the following
format statement:
@f beta TeX
@ This is the result. The identifier |alpha| is not treated specially,
so it is printed in the usual way.
@=
phin = alpha/beta;
@ Switch statements are new in Matlab version 5. Here is a
pathological case.
@=
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,3,4}, a = 3;
otherwise
a = 4+5;
end
**