Conmessenger

From Bradwiki
Jump to navigation Jump to search


Here's a little tutorial on how to setup and display code annotation and other messages on a figure instead of sending them to the console.

preview

<mediaplayer image='http://www.bradleymonk.com/w/images/5/5b/Conmessenger.png' width='900' height='600'>http://youtu.be/SuAq2y8xt8s</mediaplayer>


tutorial source code

  1 %% -- FIGURE SETUP FOR CON MESSAGING
  2 clc; close all; clear all;
  3 fh1 = figure(1);
  4 set(fh1,'OuterPosition',[550 400 1100 700],'Color',[1,1,1],'Tag','fh1')
  5 hax1 = axes('Position',[.05 .22 .44 .7],'Color','none','XTick',[],'YTick',[]);
  6 hax2 = axes('Position',[.53 .22 .44 .7],'Color','none','XTick',[],'YTick',[]);
  7 
  8 
  9 %% -- SET ANNOTATION AREA IN FIGURE
 10 
 11 sp=sprintf(' ');sp1=sprintf('>>'); sp2=sprintf('>>');sp3=sprintf('>>');sp4=sprintf('>>');
 12 str = {' ', sp1,sp2,sp3,sp4};
 13 ft = annotation(fh1,'textbox', [0.05,0.03,0.7,0.13],'String', str,'FontSize',14);
 14 set(ft,'interpreter','none')
 15 
 16 
 17 %% -- CREATE ANON FUNCTION FOR CON MESSAGING
 18 upcon = @(ft,sp,spf,sp2,sp3,sp4) ...
 19 eval('evalin(''caller'',''sp1=sp2;''); evalin(''caller'',''sp2=sp3;''); evalin(''caller'',''sp3=sp4;''); evalin(''caller'',''sp4=spf;''); evalin(''caller'',''ft.String={sp,sp1,sp2,sp3,sp4};'');');
 20 
 21 
 22 %% -- WRITE STUFF TO CON
 23 
 24 
 25 % COUNT DOWN TO LAUNCH
 26 for x = 1:5;
 27     %--- PRINT MESSAGE TO CON ---
 28     spf = sprintf('>> launching in... % 6.4g ',6-x);
 29     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.3);
 30     %----------------------------
 31 end
 32 
 33 
 34 
 35 % PRINT HELLO WORLD
 36 thismessage = 'world';
 37 
 38     %--- PRINT MESSAGE TO CON ---
 39     spf=sprintf('hello %s !',thismessage);
 40     upcon(ft,sp,spf,sp2,sp3,sp4); pause(1);
 41     %----------------------------
 42 
 43 
 44 
 45 % PLOT STUFF AND EXPLAIN WHAT'S HAPPENING IN CON
 46 
 47     %--- PRINT MESSAGE TO CON ---
 48     spf=sprintf('world, let''s plot something');
 49     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
 50     %----------------------------
 51 
 52     %--- PRINT MESSAGE TO CON ---
 53     spf=sprintf('how about a delaunay triangulation?');
 54     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
 55     %----------------------------
 56 
 57     %--- PRINT MESSAGE TO CON ---
 58     spf=sprintf('first we need some random points');
 59     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
 60     %----------------------------
 61 
 62 x = rand(10,1);
 63 y = rand(10,1);
 64 
 65     %--- PRINT MESSAGE TO CON ---
 66     spf = sprintf('>> x = rand(10,1)');
 67     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.3);
 68     spf = sprintf('% 4.2g ',x);
 69     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
 70     %----------------------------
 71 
 72     %--- PRINT MESSAGE TO CON ---
 73     spf = sprintf('>> y = rand(10,1)');
 74     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.3);
 75     spf = sprintf('% 4.2g ',y);
 76     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
 77     %----------------------------
 78 
 79     %--- PRINT MESSAGE TO CON ---
 80     spf=sprintf('now to perform the delaunay triangulation');
 81     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
 82     %----------------------------
 83 
 84 
 85 dt = delaunayTriangulation(x,y)
 86 
 87     %--- PRINT MESSAGE TO CON ---
 88     spf=sprintf('dt = delaunayTriangulation(x,y);');
 89     upcon(ft,sp,spf,sp2,sp3,sp4); pause(1);
 90     %----------------------------
 91 
 92 
 93 % I'M STILL WORKING ON THIS ONE - 
 94 % ANYONE HAVE A BETTER IDEA FOR DISPLAYING 'dt' IN THE FIGURE CON
 95 % OTHER THAN SOMETHING LIKE THIS...
 96 %{
 97 % Get instances of command window text
 98 jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
 99 jCmdWin = jDesktop.getClient('Command Window');
100 jTextArea = jCmdWin.getComponent(0).getViewport.getView;
101 cwText = char(jTextArea.getText);
102 display(dt)
103 jTextArea = jCmdWin.getComponent(0).getViewport.getView;
104 cwText = char(jTextArea.getText);
105     %--- PRINT MESSAGE TO CON ---
106     spf=sprintf('dt parameters: %s',cwText);
107     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.5);
108     %----------------------------
109 %}
110 
111 
112     %--- PRINT MESSAGE TO CON ---
113     spf=sprintf('lets plot this using triplot(dt)');
114     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
115     %----------------------------
116 
117     axes(hax1)
118 triplot(dt)
119     hold on
120 
121     %--- PRINT MESSAGE TO CON ---
122     spf=sprintf('ok, just evoked triplot(dt)');
123     upcon(ft,sp,spf,sp2,sp3,sp4); pause(1.5);
124     %----------------------------
125 
126     %--- PRINT MESSAGE TO CON ---
127     spf=sprintf('now lets apply labels to the vertices and triangles');
128     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3.5);
129     %----------------------------
130 
131 
132 vxlabels = arrayfun(@(n) {sprintf('P%d', n)}, (1:10)');
133 Hpl = text(x, y, vxlabels, 'FontWeight', 'bold', 'HorizontalAlignment',...
134    'center', 'BackgroundColor', 'none');
135 
136     %--- PRINT MESSAGE TO CON ---
137     spf=sprintf('just added vertex labels to the plot');
138     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
139     %----------------------------
140 
141     %--- PRINT MESSAGE TO CON ---
142     spf=sprintf('now to apply triangle labels...');
143     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
144     %----------------------------
145 
146 
147 ic = incenter(dt);
148 numtri = size(dt,1);
149 trilabels = arrayfun(@(x) {sprintf('T%d', x)}, (1:numtri)');
150 Htl = text(ic(:,1), ic(:,2), trilabels, 'FontWeight', 'bold', ...
151    'HorizontalAlignment', 'center', 'Color', 'blue');
152 hold off
153 
154     %--- PRINT MESSAGE TO CON ---
155     spf=sprintf('now to apply triangle labels...done');
156     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
157     %----------------------------
158 
159 
160 
161     %--- PRINT MESSAGE TO CON ---
162     spf=sprintf(' '); upcon(ft,sp,spf,sp2,sp3,sp4);
163     spf=sprintf('that was fun; lets do it again in 3D');
164     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
165     spf=sprintf(' '); upcon(ft,sp,spf,sp2,sp3,sp4);
166     %----------------------------
167 
168 
169     %--- PRINT MESSAGE TO CON ---
170     spf=sprintf('first lets get some arbitrary data from the gallery...');
171     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
172     %----------------------------
173 
174 x = gallery('uniformdata',[30 1],0);
175 y = gallery('uniformdata',[30 1],1);
176 z = gallery('uniformdata',[30 1],2);
177 
178 
179     %--- PRINT MESSAGE TO CON ---
180     spf=sprintf('>> x = gallery(''uniformdata'',[30 1],0);');
181     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.4);
182     spf=sprintf('>> y = gallery(''uniformdata'',[30 1],1);');
183     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.4);
184     spf=sprintf('>> z = gallery(''uniformdata'',[30 1],2);');
185     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
186     %----------------------------
187 
188 
189     %--- PRINT MESSAGE TO CON ---
190     spf=sprintf('again we can use the delaunayTriangulation() function');
191     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
192     %----------------------------
193 
194 
195 DT = delaunayTriangulation(x,y,z)
196 
197 
198     %--- PRINT MESSAGE TO CON ---
199     spf=sprintf('>> DT = delaunayTriangulation(x,y,z)');
200     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
201     %----------------------------
202 
203     %--- PRINT MESSAGE TO CON ---
204     spf=sprintf('time to plot using tetramesh(DT)');
205     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
206     %----------------------------
207 
208     axes(hax2)
209 tetramesh(DT);
210 
211     %--- PRINT MESSAGE TO CON ---
212     spf=sprintf('wow...');
213     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
214     %----------------------------
215 
216     %--- PRINT MESSAGE TO CON ---
217     spf=sprintf('that is one ugly polyhedron');
218     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
219     %----------------------------
220 
221     %--- PRINT MESSAGE TO CON ---
222     spf=sprintf('lets specify some face colors');
223     upcon(ft,sp,spf,sp2,sp3,sp4); pause(3);
224     %----------------------------
225 
226 faceColor  = [0.6875 0.8750 0.8984];
227 
228     %--- PRINT MESSAGE TO CON ---
229     spf=sprintf('>> faceColor  = [0.6875 0.8750 0.8984];');
230     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
231     %----------------------------
232 
233     %--- PRINT MESSAGE TO CON ---
234     spf=sprintf('>> tetramesh(DT,''FaceColor'',faceColor,''FaceAlpha'',0.3);');
235     upcon(ft,sp,spf,sp2,sp3,sp4); pause(.1);
236     %----------------------------
237 
238     axes(hax2)
239 tetramesh(DT,'FaceColor',faceColor,'FaceAlpha',0.3);
240 
241 
242     %--- PRINT MESSAGE TO CON ---
243     spf=sprintf('fantastic');
244     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
245     %----------------------------
246 
247     %--- PRINT MESSAGE TO CON ---
248     spf=sprintf('lets rotate this son-of-a-gun...');
249     upcon(ft,sp,spf,sp2,sp3,sp4); pause(2);
250     %----------------------------
251 
252 
253 hax2view = hax2.View;
254 
255 for vv = 1:360
256 
257     view([hax2view(1)+vv hax2view(2)])
258     pause(.05)
259 
260     if vv == 100
261     %--- PRINT MESSAGE TO CON ---
262     spf=sprintf('con messaging lets users know whats happening under the hood');
263     upcon(ft,sp,spf,sp2,sp3,sp4);
264     %----------------------------
265     elseif vv == 200
266     %--- PRINT MESSAGE TO CON ---
267     spf=sprintf('and reinforces good literate programming practices');
268     upcon(ft,sp,spf,sp2,sp3,sp4);
269     %----------------------------
270     elseif vv == 300
271     %--- PRINT MESSAGE TO CON ---
272     spf=sprintf('but remember - everything in moderation');
273     upcon(ft,sp,spf,sp2,sp3,sp4);
274     %----------------------------
275     end
276 
277 end
278 
279 
280 %--- PRINT MESSAGE TO CON ---
281 spf=sprintf('thanks for checking out my tutorial!');
282 upcon(ft,sp,spf,sp2,sp3,sp4); pause(5);
283 %----------------------------
284 
285 close(gcf)