summaryrefslogtreecommitdiff
path: root/web/matlabweb/example.web
blob: 7dea51b5899657a775cf7ccc5e95aaad7a799d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
%
% $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');
  @<set the callbacks@>


@*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 the callbacks@>=
   set(gcf,'WindowButtonDownFcn', `
      @<create the line@>
      set(gcf,"WindowButtonMotionFcn", `@<reset the line@>');
      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.

@<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 = set(L, "Color", "r");



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

@u
   @<unused code@>
   @<create the line@>
   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.

@<unused code@>=
  a = 2i;
  z = 2e4 + 3e4i;
  
  flarp = rand(3,8) ./ linspace(3,8);
  @<some refinement@>


@ 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);')
@<unused code@>=

  print_arg(gcf, disp, "aargh!");
  print_arg(gca, fprintf, "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 a==4, a = b/2; else a = a+1;  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


@ A terrible |for| statement:
@.``at-period'' index entry@>
@^``at-hat'' index entry@>
@<some refinement@>=

   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.

@<unused code@>=

   phin = alpha/beta;
   
   
@ Switch statements are new in Matlab version 5. Here is a
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,3,4},  a = 3;
  otherwise
    a = 4+5;
end