summaryrefslogtreecommitdiff
path: root/web/matlabweb/test.web
blob: 99215d7743f299bd62e40b196a7d4bb858d88bce (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
\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.