<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5976912739175151841</id><updated>2011-11-27T15:26:22.725-08:00</updated><category term='matlab'/><category term='graphic'/><category term='simulink'/><category term='plot'/><category term='image processing'/><category term='matlab basic'/><category term='gui'/><category term='serial port'/><title type='text'>Matlab Programming</title><subtitle type='html'>Free Matlab programming tips and tricks with free source code available for download</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5976912739175151841.post-2259911880402042262</id><published>2009-02-09T01:56:00.000-08:00</published><updated>2009-02-09T04:47:08.601-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='graphic'/><category scheme='http://www.blogger.com/atom/ns#' term='plot'/><category scheme='http://www.blogger.com/atom/ns#' term='matlab'/><title type='text'>Ploting Axes in Matlab</title><content type='html'>&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;1. Initial&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as&lt;br /&gt;annotating and printing these graphs. This section describes a few of the most important graphics functions and provides examples of some typical applications.&lt;br /&gt;&lt;br /&gt;The &lt;code style="color: rgb(51, 102, 255);"&gt;plot&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;function has different forms, depending on the input arguments. If &lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt;&lt;em style="color: rgb(51, 102, 255);"&gt; &lt;/em&gt;is a vector, &lt;code style="color: rgb(51, 102, 255);"&gt;plot(y)&lt;/code&gt; produces a piecewise linear graph of the elements of &lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;versus the index of the elements of &lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt;. If you specify two vectors as arguments, &lt;code style="color: rgb(51, 102, 255);"&gt;plot(x,y)&lt;/code&gt; produces a graph of &lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;versus &lt;code style="color: rgb(51, 102, 255);"&gt;x&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;For example, to plot the value of the sine function from zero to 2&lt;img src="http://web.missouri.edu/%7Exiaoc/basics/pi.gif" align="absmiddle" /&gt;, use&lt;br /&gt;&lt;pre style="color: rgb(51, 102, 255);"&gt;t = 0:pi/100:2*pi;&lt;br /&gt;&lt;a name="988"&gt;&lt;/a&gt;y = sin(t);&lt;br /&gt;&lt;a name="990"&gt;&lt;/a&gt;plot(t,y)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CzN3cg4BCxs/SY_-ruUn_XI/AAAAAAAAAJU/WvFdldkilJ0/s1600-h/plot1.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 311px;" src="http://3.bp.blogspot.com/_CzN3cg4BCxs/SY_-ruUn_XI/AAAAAAAAAJU/WvFdldkilJ0/s400/plot1.gif" alt="" id="BLOGGER_PHOTO_ID_5300735313560730994" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Multiple &lt;code style="color: rgb(51, 102, 255);"&gt;x&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;-&lt;/span&gt;&lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt; pairs create multiple graphs with a single call to &lt;code style="color: rgb(51, 102, 255);"&gt;plot&lt;/code&gt;. MATLAB automatically cycles through a predefined (but user settable) list of colors to allow discrimination between each set of data. For example, these statements plot three related functions of &lt;code style="color: rgb(51, 102, 255);"&gt;t&lt;/code&gt;, each curve in a separate distinguishing color: &lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;y2 = sin(t-.25);&lt;/span&gt; &lt;a style="color: rgb(51, 102, 255);" name="996"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;y3 = sin(t-.5);&lt;/span&gt; &lt;a style="color: rgb(51, 102, 255);" name="998"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;plot(t,y,t,y2,t,y3)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SZACQV8Nt_I/AAAAAAAAAJs/69vakCo0dNc/s1600-h/plot2.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 300px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SZACQV8Nt_I/AAAAAAAAAJs/69vakCo0dNc/s400/plot2.jpg" alt="" id="BLOGGER_PHOTO_ID_5300739241205938162" border="0" /&gt;&lt;/a&gt;It is possible to specify color, linestyle, and markers, such as plus signs or circles, with:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;plot(x,y,'&lt;/span&gt;&lt;em style="color: rgb(51, 102, 255);"&gt;color_style_marker&lt;/em&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;code style="color: rgb(51, 102, 255);"&gt;color_style_marker&lt;/code&gt; is a 1-, 2-, or 3-character string (delineated by single quotation marks) constructed from a color, a linestyle, and a marker type:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a name="1006"&gt;Color strings are &lt;code&gt;'c'&lt;/code&gt;, &lt;code&gt;'m'&lt;/code&gt;, &lt;code&gt;'y'&lt;/code&gt;, &lt;code&gt;'r'&lt;/code&gt;, &lt;code&gt;'g'&lt;/code&gt;, &lt;code&gt;'b'&lt;/code&gt;, &lt;code&gt;'w'&lt;/code&gt;, and &lt;code&gt;'k'&lt;/code&gt;. These correspond to cyan, magenta, yellow, red, green, blue, white, and black. &lt;/a&gt;&lt;a name="1008"&gt; &lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a name="1008"&gt;Linestyle strings are &lt;code&gt;'-'&lt;/code&gt; for solid, &lt;code&gt;'&lt;/code&gt;- -&lt;code&gt;'&lt;/code&gt; for dashed, &lt;code&gt;':'&lt;/code&gt; for dotted, &lt;code&gt;'-.'&lt;/code&gt; for dash-dot, and &lt;code&gt;'none'&lt;/code&gt; for no line. &lt;/a&gt;&lt;a name="1010"&gt; &lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a name="1010"&gt;The most common marker types include &lt;code&gt;'+'&lt;/code&gt;, &lt;code&gt;'o'&lt;/code&gt;, &lt;code&gt;'*'&lt;/code&gt;, and &lt;code&gt;'x'&lt;/code&gt;. &lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;For example, the statement:&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;span style="font-family:monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;pre style="color: rgb(51, 102, 255);"&gt;plot(x,y,'y:+')&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);font-size:130%;" &gt;&lt;span style="font-family:georgia;"&gt;plots a yellow dotted line and places plus sign markers at each data &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;point. If you specify a marker type but not a linestyle, MATLAB draws&lt;/span&gt; &lt;span style="font-family:georgia;"&gt;only the marker.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;span style="font-family: georgia;font-size:130%;" &gt;&lt;span style="font-weight: bold;"&gt;2. Figure Windows&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The &lt;code&gt;plot&lt;/code&gt; function automatically opens a new figure window if there are no figure windows already on the screen. If a figure window exists, &lt;code&gt;plot&lt;/code&gt; uses that window by default.   To open a new figure window and make it the current figure, type&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;figure&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;To make an existing figure window the current figure, type&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;figure(n)&lt;/span&gt;&lt;/pre&gt; where &lt;code&gt;n&lt;/code&gt; is the number in the figure title bar. The results of subsequent graphics commands are displayed in this window.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;3. Adding Plots to an Existing Graph&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The &lt;code style="color: rgb(51, 102, 255);"&gt;hold&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;command allows you to add plots to an existing graph. When you type&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;hold on&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:georgia;"&gt;MATLAB does not remove the existing graph; it adds the new data to the current graph, rescaling if necessary. For example, these statements&lt;/span&gt; &lt;span style="font-family:georgia;"&gt;first create a contour plot of the &lt;/span&gt;&lt;code style="font-family: georgia;"&gt;peaks&lt;/code&gt;&lt;span style="font-family:georgia;"&gt; function, then superimpose a pseudocolor plot of the same function:&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;[x,y,z] = peaks;&lt;/span&gt; &lt;a style="color: rgb(51, 102, 255);" name="7597"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;contour(x,y,z,20,'k')&lt;/span&gt; &lt;a style="color: rgb(51, 102, 255);" name="7598"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;hold on&lt;/span&gt; &lt;a style="color: rgb(51, 102, 255);" name="7599"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;pcolor(x,y,z)&lt;/span&gt; &lt;a style="color: rgb(51, 102, 255);" name="7600"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;shading interp&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:georgia;"&gt;The &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-size:100%;" &gt;&lt;code style="font-family: courier new;"&gt;hold on&lt;/code&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt; command causes the &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-size:100%;" &gt;&lt;code style="font-family: courier new;"&gt;pcolor&lt;/code&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 102, 255);font-size:100%;" &gt;&lt;span style="font-family:courier new;"&gt; &lt;/span&gt;&lt;/span&gt;plot to be combined with the &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-size:100%;" &gt;&lt;code style="font-family: courier new;"&gt;contour&lt;/code&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 102, 255);font-size:100%;" &gt;&lt;span style="font-family:courier new;"&gt; &lt;/span&gt;&lt;/span&gt;plot in one figure.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SZAFHgutefI/AAAAAAAAAJ0/xVD46OIKNus/s1600-h/plot3.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 321px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SZAFHgutefI/AAAAAAAAAJ0/xVD46OIKNus/s400/plot3.gif" alt="" id="BLOGGER_PHOTO_ID_5300742388018149874" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;4. Subplots&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;The &lt;code style="color: rgb(51, 102, 255);"&gt;subplot&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;function allows you to display multiple plots in the same window or print them on the same piece of paper. Typing&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;subplot(m,n,p)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;breaks the figure window into an &lt;code&gt;m&lt;/code&gt;-by-&lt;code&gt;n&lt;/code&gt; matrix of small subplots and selects the &lt;code&gt;p&lt;/code&gt;th subplot for the current plot. The plots are numbered along first the top row of the figure window, then the second row, and so on. For example, to plot data in four different subregions of the figure window,&lt;br /&gt;&lt;pre style="color: rgb(51, 102, 255);"&gt;t = 0:pi/10:2*pi;&lt;br /&gt;&lt;a name="1060"&gt;&lt;/a&gt;[X,Y,Z] = cylinder(4*cos(t));&lt;br /&gt;&lt;a name="1062"&gt;&lt;/a&gt;subplot(2,2,1); &lt;a name="1064"&gt;&lt;/a&gt;mesh(X)&lt;br /&gt;&lt;a name="1068"&gt;&lt;/a&gt;subplot(2,2,2); mesh(Y)&lt;br /&gt;&lt;a name="1072"&gt;&lt;/a&gt;subplot(2,2,3); mesh(Z)&lt;br /&gt;&lt;a name="1076"&gt;&lt;/a&gt;subplot(2,2,4); mesh(X,Y,Z)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SZAGutXdA-I/AAAAAAAAAJ8/ELlRQyxYTvU/s1600-h/plot4.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 302px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SZAGutXdA-I/AAAAAAAAAJ8/ELlRQyxYTvU/s400/plot4.gif" alt="" id="BLOGGER_PHOTO_ID_5300744160936788962" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;5. Imaginary and Complex Data&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;When the arguments to &lt;code style="color: rgb(51, 102, 255);"&gt;plot&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;are complex, the imaginary part is ignored&lt;em&gt; except&lt;/em&gt; when &lt;code style="color: rgb(51, 102, 255);"&gt;plot&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;is given a single complex argument. For this special case, the command is a shortcut for a plot of the real part versus the imaginary part. Therefore,&lt;br /&gt;&lt;pre style="color: rgb(51, 102, 255);"&gt;plot(Z)&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="color: rgb(0, 0, 0);"&gt;where &lt;/span&gt;&lt;code style="color: rgb(51, 102, 255);"&gt;Z&lt;/code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;is a complex vector or matrix, is equivalent to&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;plot(real(Z),imag(Z))&lt;br /&gt;&lt;/pre&gt;For example &lt;pre style="color: rgb(51, 102, 255);"&gt;t = 0:pi/10:2*pi;&lt;br /&gt;&lt;a name="1096"&gt;&lt;/a&gt;plot(exp(i*t),'-o')&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="color: rgb(0, 0, 0);"&gt;draws a 20-sided polygon with little circles at the vertices.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SZAdggA49WI/AAAAAAAAAKE/3TAOY0-aKto/s1600-h/plot5.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 382px; height: 370px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SZAdggA49WI/AAAAAAAAAKE/3TAOY0-aKto/s400/plot5.gif" alt="" id="BLOGGER_PHOTO_ID_5300769205601760610" border="0" /&gt;&lt;/a&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;6. Controlling Axes&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The &lt;code style="color: rgb(51, 102, 255);"&gt;axis&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;function has a number of options for customizing the scaling, orientation, and aspect ratio of plots.&lt;br /&gt;&lt;br /&gt;Ordinarily, MATLAB finds the maxima and minima of the data and chooses an appropriate plot box and axes labeling. The &lt;code&gt;axis&lt;/code&gt; function overrides the default by setting custom axis limits,&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;axis([xmin xmax ymin ymax])&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;code style="color: rgb(51, 102, 255);"&gt;axis&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;also accepts a number of keywords for axes control.   For example&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;axis square&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;makes the entire &lt;em&gt;x&lt;/em&gt;-axes and &lt;em&gt;y&lt;/em&gt;-axes the same length and&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;axis equal&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;makes the individual tick mark increments on the &lt;em&gt;x&lt;/em&gt;- and &lt;em&gt;y&lt;/em&gt;-axes the same length. So&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;plot(exp(i*t))&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;followed by either &lt;code style="color: rgb(51, 102, 255);"&gt;axis square&lt;/code&gt; or &lt;code style="color: rgb(51, 102, 255);"&gt;axis equal&lt;/code&gt; turns the oval into a proper circle.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;axis auto&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;returns the axis scaling to its default, automatic mode.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;axis on&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;turns on axis labeling and tick marks.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;axis off&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;turns off axis labeling and tick marks.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;The statement&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;grid off&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;a name="1138"&gt;&lt;/a&gt;turns the grid lines off and&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;grid on&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;turns them back on again.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;7. Axis Labels and Titles&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The &lt;code style="color: rgb(51, 102, 255);"&gt;xlabel&lt;/code&gt;, &lt;code style="color: rgb(51, 102, 255);"&gt;ylabel&lt;/code&gt;, and &lt;code style="color: rgb(51, 102, 255);"&gt;zlabel&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;functions add &lt;em&gt;x&lt;/em&gt;-, &lt;em&gt;y&lt;/em&gt;-, and &lt;em&gt;z&lt;/em&gt;-axis labels. The &lt;code style="color: rgb(51, 102, 255);"&gt;title&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;function adds a title at the top of the figure and the &lt;code style="color: rgb(51, 102, 255);"&gt;text&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;function inserts text anywhere in the figure. A subset of Tex notation produces Greek letters, mathematical symbols, and alternate fonts. The following example uses &lt;code style="color: rgb(51, 102, 255);"&gt;\leq&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;for &lt;img src="http://web.missouri.edu/%7Exiaoc/basics/lt_equal.gif" align="absmiddle" /&gt;, &lt;code style="color: rgb(51, 102, 255);"&gt;\pi&lt;/code&gt; for &lt;img src="http://web.missouri.edu/%7Exiaoc/basics/pi.gif" align="absmiddle" /&gt;, and &lt;code style="color: rgb(51, 102, 255);"&gt;\it&lt;/code&gt; for italic font.&lt;br /&gt;&lt;pre style="color: rgb(51, 102, 255);"&gt;t = -pi:pi/100:pi;&lt;br /&gt;&lt;a name="1152"&gt;&lt;/a&gt;y = sin(t);&lt;br /&gt;&lt;a name="1154"&gt;&lt;/a&gt;plot(t,y)&lt;br /&gt;&lt;a name="1156"&gt;&lt;/a&gt;axis([-pi pi -1 1])&lt;br /&gt;&lt;a name="1158"&gt;&lt;/a&gt;xlabel('-\pi \leq {\itt} \leq \pi')&lt;br /&gt;&lt;a name="1160"&gt;&lt;/a&gt;ylabel('sin(t)')&lt;br /&gt;&lt;a name="1162"&gt;&lt;/a&gt;title('Graph of the sine function')&lt;br /&gt;&lt;a name="1164"&gt;&lt;/a&gt;text(1,-1/3,'\it{Note the odd symmetry.}')&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SZAgFBiXLsI/AAAAAAAAAKM/zKJYM455DC4/s1600-h/plot6.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 327px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SZAgFBiXLsI/AAAAAAAAAKM/zKJYM455DC4/s400/plot6.gif" alt="" id="BLOGGER_PHOTO_ID_5300772032099069634" border="0" /&gt;&lt;/a&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;8. Mesh and Surface Plots&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;MATLAB defines a surface by the &lt;em&gt;z&lt;/em&gt;-coordinates of points above a grid in the &lt;em&gt;x&lt;/em&gt;-&lt;em&gt;y&lt;/em&gt; plane, using straight lines to connect adjacent points. The functions &lt;code style="color: rgb(51, 102, 255);"&gt;mesh&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;and &lt;code style="color: rgb(51, 102, 255);"&gt;surf&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;display surfaces in three dimensions. &lt;code style="color: rgb(51, 102, 255);"&gt;mesh&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;produces wireframe surfaces that color only the lines connecting the defining points. &lt;code style="color: rgb(51, 102, 255);"&gt;surf&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;displays both the connecting lines and the faces of the surface in color.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;9. Visualizing Functions of Two Variables&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;To display a function of two variables, &lt;em&gt;z&lt;/em&gt; = &lt;em&gt;f &lt;/em&gt;(&lt;em&gt;x&lt;/em&gt;,&lt;em&gt;y&lt;/em&gt;), generate &lt;code style="font-family: georgia;"&gt;X&lt;/code&gt; and &lt;code style="font-family: georgia;"&gt;Y&lt;/code&gt; matrices consisting of repeated rows and columns, respectively, over the domain of the function. Then use these matrices to evaluate and graph the function. The &lt;code style="color: rgb(51, 102, 255);"&gt;meshgrid&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;function transforms the domain specified by a single vector or two vectors &lt;code style="color: rgb(51, 102, 255);"&gt;x&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;and &lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;into matrices &lt;code style="font-family: georgia;"&gt;X&lt;/code&gt; and &lt;code style="font-family: georgia;"&gt;Y&lt;/code&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;for use in evaluating functions of two variables. The rows of &lt;code style="font-family: georgia;"&gt;X&lt;/code&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;are copies of the vector &lt;code style="color: rgb(51, 102, 255);"&gt;x&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;and the columns of &lt;span style="font-size:100%;"&gt;&lt;code style="font-family: georgia;"&gt;Y&lt;/code&gt;&lt;/span&gt; are copies of the vector &lt;code style="color: rgb(51, 102, 255);"&gt;y&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;To evaluate the two-dimensional &lt;em&gt;sinc&lt;/em&gt; function, sin(&lt;em&gt;r&lt;/em&gt;)/&lt;em&gt;r&lt;/em&gt;, between &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; directions:&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;span style="font-family:monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;[X,Y] = meshgrid(-8:.5:8);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:courier new;font-size:85%;"  &gt;&lt;a style="color: rgb(51, 102, 255);" name="1178"&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;R = sqrt(X.^2 + Y.^2) + eps;&lt;/span&gt;&lt;br /&gt;&lt;a style="color: rgb(51, 102, 255); font-family: courier new;" name="1180"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;Z = sin(R)./R;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;a style="color: rgb(51, 102, 255); font-family: courier new;" name="1182"&gt;&lt;/a&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;mesh(X,Y,Z)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CzN3cg4BCxs/SZAhews9INI/AAAAAAAAAKU/6NzBd6TZWLw/s1600-h/plot7.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 294px;" src="http://3.bp.blogspot.com/_CzN3cg4BCxs/SZAhews9INI/AAAAAAAAAKU/6NzBd6TZWLw/s400/plot7.gif" alt="" id="BLOGGER_PHOTO_ID_5300773573768323282" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;In this example, &lt;code&gt;R&lt;/code&gt; is the distance from origin, which is at the center of the matrix. Adding &lt;code&gt;eps&lt;/code&gt; avoids the indeterminate 0/0 at the origin.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;10. Images&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Two-dimensional arrays can be displayed as &lt;em&gt;images&lt;/em&gt;, where the array elements determine brightness or color of the images. For example,&lt;br /&gt;&lt;pre style="color: rgb(51, 102, 255);"&gt;load durer&lt;br /&gt;&lt;a name="1192"&gt;&lt;/a&gt;whos&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);font-family:georgia;font-size:100%;"  &gt;shows that file &lt;code style="color: rgb(51, 102, 255);"&gt;durer.mat&lt;/code&gt; in the demo directory contains a 648-by-509 matrix, &lt;code&gt;X&lt;/code&gt;, and a 128-by-3 matrix, &lt;code style="color: rgb(51, 102, 255);"&gt;map&lt;/code&gt;. The elements of &lt;code&gt;X&lt;/code&gt; are integers between 1 and 128, which serve as indices into the color map, &lt;code style="color: rgb(51, 102, 255);"&gt;map&lt;/code&gt;. Then&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style="color: rgb(51, 102, 255);font-size:85%;" &gt;&lt;span style="font-family:courier new;"&gt;image(X)&lt;/span&gt;&lt;br /&gt;&lt;a style="font-family: courier new;" name="1198"&gt;&lt;/a&gt;&lt;span style="font-family:courier new;"&gt;colormap(map)&lt;/span&gt;&lt;br /&gt;&lt;a style="font-family: courier new;" name="1200"&gt;&lt;/a&gt;&lt;span style="font-family:courier new;"&gt;axis image&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;reproduces Dürer's etching shown at the beginning of this book. A high resolution scan of the magic square in the upper right corner is available in another file. Type&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;load detail&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;and then use the uparrow key on your keyboard to reexecute the &lt;code&gt;image&lt;/code&gt;, &lt;code&gt;colormap&lt;/code&gt;, and &lt;code&gt;axis&lt;/code&gt; commands. The statement&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;colormap(hot) &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;adds some twentieth century colorization to the sixteenth century etching.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;11. Printing Graphics&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;The &lt;strong&gt;Print&lt;/strong&gt; option on the &lt;strong&gt;File&lt;/strong&gt; menu and the &lt;code style="color: rgb(51, 102, 255);"&gt;print&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;command both print MATLAB figures. The &lt;strong&gt;Print&lt;/strong&gt; menu brings up a dialog box that lets you select common standard printing options. The &lt;code style="color: rgb(51, 102, 255);"&gt;print&lt;/code&gt;&lt;span style="color: rgb(51, 102, 255);"&gt; &lt;/span&gt;command provides more flexibility in the type of output and allows you to control printing from M-files. The result can be sent directly to your default printer or stored in a specified file. A wide variety of output formats, including PostScript, is available.&lt;br /&gt;&lt;br /&gt;For example, this statement saves the contents of the current figure window as color Encapsulated Level 2 PostScript in the file called &lt;code style="color: rgb(51, 102, 255);"&gt;magicsquare.eps&lt;/code&gt;:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;print -depsc2 magicsquare.eps&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;It's important to know the capabilities of your printer before using the &lt;code style="color: rgb(51, 102, 255);"&gt;print &lt;/code&gt;command. For example, Level 2 Postscript files are generally smaller&lt;br /&gt;and render more quickly when printing than Level 1 Postscript. However, not all PostScript printers support Level 2, so you need to know what&lt;br /&gt;your output device can handle. MATLAB produces graduated output for surfaces and patches, even for black and white output devices. However,&lt;br /&gt;lines and text are printed in black or white.&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5976912739175151841-2259911880402042262?l=programmingmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/2259911880402042262/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5976912739175151841&amp;postID=2259911880402042262' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/2259911880402042262'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/2259911880402042262'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/2009/02/ploting-axes-in-matlab.html' title='Ploting Axes in Matlab'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_CzN3cg4BCxs/SY_-ruUn_XI/AAAAAAAAAJU/WvFdldkilJ0/s72-c/plot1.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5976912739175151841.post-9083425247071216181</id><published>2009-02-05T09:46:00.000-08:00</published><updated>2009-02-05T10:15:57.535-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='matlab'/><category scheme='http://www.blogger.com/atom/ns#' term='simulink'/><title type='text'>Simulink Introduction</title><content type='html'>&lt;b&gt;Simulink&lt;/b&gt; (&lt;b&gt;Simu&lt;/b&gt;lation and &lt;b&gt;Link&lt;/b&gt;) is an extension of MATLAB. It works with MATLAB to offer modeling, simulating, and analyzing of dynamical systems under a graphical user interface (GUI) environment.  The construction of a model is simplified with click-and-drag mouse operations. Simulink includes a comprehensive block library of toolboxes for both linear and nonlinear analysis. Models are hierarchical, which allow using both top-down and bottom-up approaches. As Simulink is an integral part of MATLAB, it is easy to switch back and forth during the analysis process and thus, the user may take full advantage of features offered in both environments.  This tutorial presents the basic features of Simulink and is focused on control systems. Very useful for engineering field. This tutorial has been &lt;span style="font-family:georgia;"&gt;writte&lt;/span&gt;n  for Simulink v.5 and v.6 It is upward and backward version compatible.&lt;br /&gt;&lt;br /&gt;&lt;p style="font-weight: bold; color: rgb(0, 0, 0);"&gt;&lt;span style=";font-family:Verdana,Arial,Helvetica,sans-serif;font-size:85%;"  &gt;Getting Started &lt;/span&gt; &lt;/p&gt;           &lt;p class="textstyle"&gt;&lt;span class="textstyle"&gt;To start a Simulink session, you'd need to bring up Matlab program first.&lt;/span&gt;&lt;/p&gt;           &lt;p class="textstyle"&gt;&lt;span class="textstyle"&gt;From Matlab command window, enter:&lt;/span&gt;&lt;/p&gt;           &lt;p  style="color: rgb(51, 102, 255);font-family:courier new;" class="textstyle"&gt;&lt;b&gt;&gt;&gt; simulink&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p  style="color: rgb(51, 102, 255);font-family:courier new;" class="textstyle"&gt;&lt;span style="color: rgb(0, 0, 0);font-family:georgia;" &gt;Alternately, you may click on the Simulink icon located on the toolbar as shown:&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYsnbvNqtRI/AAAAAAAAAHY/RMvlRKoHPk8/s1600-h/simulinkicon.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 210px; height: 61px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYsnbvNqtRI/AAAAAAAAAHY/RMvlRKoHPk8/s400/simulinkicon.jpg" alt="" id="BLOGGER_PHOTO_ID_5299372744015262994" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Simulink's library browser window like   one shown below will pop up presenting the block set for model   construction.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYsnyxCbaSI/AAAAAAAAAHg/dEYkww3g6uY/s1600-h/libbrowser.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 363px; height: 400px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYsnyxCbaSI/AAAAAAAAAHg/dEYkww3g6uY/s400/libbrowser.jpg" alt="" id="BLOGGER_PHOTO_ID_5299373139641985314" border="0" /&gt;&lt;/a&gt;To see the content of the blockset, click on the "+" sign at the beginning of each toolbox. To start a model click on the NEW FILE ICON as shown in the screenshot above. Alternately, you may use keystrokes&lt;b&gt; CTRL+N&lt;/b&gt;. A new window will appear on the screen. You will be constructing your model in this window. Also in this window the constructed model is simulated. A screenshot of a typical working (model) window is shown below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYsokvOstHI/AAAAAAAAAHo/O-DV5-9tZU0/s1600-h/workspacew.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 385px; height: 215px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYsokvOstHI/AAAAAAAAAHo/O-DV5-9tZU0/s400/workspacew.jpg" alt="" id="BLOGGER_PHOTO_ID_5299373998150038642" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;To become familiarized with the structure and the environment of Simulink, you are encouraged to explore the toolboxes and scan their contents. You may not know what they are all about at first, but perhaps you could catch on the organisation of these toolboxes according to their categories. For instance, you may see that the Control System toolbox consists of the Linear Time Invariant (LTI) system library and the MATLAB functions can be found under Function and Tables of the Simulink main toolbox. A good way to learn Simulink (or any computer program in general) is to practice and explore. Making mistakes is part of the learning curve. So, fear not you should be!&lt;br /&gt;A simple model is used here to introduce some basic features of Simulink. Please follow the steps below to construct a simple model.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;STEP 1: CREATING BLOCKS.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;From BLOCK SET CATEGORIES section of the SIMULINK LIBRARY BROWSER window, click on the "+" sign next to the &lt;b&gt;Simulink&lt;/b&gt; group to expand the tree and select (click on) &lt;b&gt;Sources&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYspIQ8SlKI/AAAAAAAAAHw/WF8Z_pR7ils/s1600-h/source.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 178px; height: 273px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYspIQ8SlKI/AAAAAAAAAHw/WF8Z_pR7ils/s400/source.jpg" alt="" id="BLOGGER_PHOTO_ID_5299374608495056034" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A set of blocks will appear in the BLOCKSET group. Click on the &lt;b&gt;Sine Wave&lt;/b&gt; block and drag it to the workspace window (also known as model window).&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYspc0Et2dI/AAAAAAAAAH4/kzB2bYIKRb4/s1600-h/drag.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 181px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYspc0Et2dI/AAAAAAAAAH4/kzB2bYIKRb4/s400/drag.jpg" alt="" id="BLOGGER_PHOTO_ID_5299374961523022290" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Now you have established a source of your model.&lt;br /&gt;&lt;br /&gt;NOTE:&lt;span style="color: rgb(0, 102, 255);"&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;It is advisable that you save your model at some point early on so that if your PC crashes you wouldn't loose too much time reconstructing your model. &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I am going to save this model under the filename: "sinexample1". To save a model, you may click on the floppy diskette icon &lt;img src="http://edu.levitas.net/Tutorials/Matlab/Simulink/simimg/saveicon.jpg" width="17" height="23" /&gt;. or from FILE menu, select &lt;b&gt;Save&lt;/b&gt; or using keystrokes CTRL+S. All Simulink model file will have an extension ".&lt;b&gt;mdl&lt;/b&gt;". Simulink recognises file with .&lt;i&gt;mdl&lt;/i&gt; extension as a simulation model (similar to how MATLAB recognises files with the extension .m as an MFile).&lt;br /&gt;Continue to build your model by adding more components (or blocks) to your model window. We'll continue to add a &lt;b&gt;Scope&lt;/b&gt; from &lt;b&gt;Sinks&lt;/b&gt; library, an &lt;b&gt;Integrator&lt;/b&gt; block from &lt;b&gt;Continuous&lt;/b&gt; library, and a &lt;b&gt;Mux&lt;/b&gt; block from &lt;b&gt;Signal Routing&lt;/b&gt; library.&lt;br /&gt;&lt;br /&gt;NOTE:&lt;i&gt; &lt;/i&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;If you wish to locate a block knowing its name, you may enter the name in the SEARCH WINDOW (at &lt;b&gt;Find&lt;/b&gt; prompt) and Simulink will bring up the specified block. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To move the blocks around, simply click on it and drag it to a desired location. Once you've dragged over all necessary blocks, the workspace window  should consist of the following components:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYsqJkRJT8I/AAAAAAAAAIA/_QPOoQ2r6Vw/s1600-h/blockset1.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 380px; height: 252px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYsqJkRJT8I/AAAAAAAAAIA/_QPOoQ2r6Vw/s400/blockset1.jpg" alt="" id="BLOGGER_PHOTO_ID_5299375730374299586" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;You may remove (delete) a block by simply clicking on it once to turn on the "select mode" (with four corner boxes) and use the DEL key or keys combination CTRL-X.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;STEP 2: MAKING CONNECTIONS&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To establish connections between the blocks, move the cursor to the output port represented by "&gt;" sign on the block. Once placed at a port, the cursor will turn into a cross "+" enabling you to make connection between blocks.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CzN3cg4BCxs/SYsqdUJsVwI/AAAAAAAAAII/IncISQFruMQ/s1600-h/sineconnected.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 344px; height: 123px;" src="http://3.bp.blogspot.com/_CzN3cg4BCxs/SYsqdUJsVwI/AAAAAAAAAII/IncISQFruMQ/s400/sineconnected.jpg" alt="" id="BLOGGER_PHOTO_ID_5299376069645457154" border="0" /&gt;&lt;/a&gt;A sine signal is generated by the Sine Wave block (a source) and is displayed by the scope. The integrated sine signal is sent to scope for display along with the original signal from the source via the &lt;b&gt;Mux&lt;/b&gt;, whose function is to mutiplex signals in form of scalar, vector, or matrix into a bus.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;STEP 3: RUNNING SIMULATION&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You now may run the simulation of the simple system above by clicking on the play button &lt;img src="http://edu.levitas.net/Tutorials/Matlab/Simulink/simimg/playsign.jpg" width="20" height="20" /&gt;. Alternately, you may use keystrokes CTRL+T, or choose Start submenu (under Simulation menu).&lt;br /&gt;Double click on the Scope block to display of the scope.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYsq00rbfNI/AAAAAAAAAIQ/N0V7XLUToY0/s1600-h/scope.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 330px; height: 294px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYsq00rbfNI/AAAAAAAAAIQ/N0V7XLUToY0/s400/scope.jpg" alt="" id="BLOGGER_PHOTO_ID_5299376473513884882" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;To view/edit the parameters, simply double click on the block of interest.&lt;br /&gt;&lt;br /&gt;&lt;b style="color: rgb(0, 0, 0);"&gt;Handling of Blocks and Lines&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The table below describes the actions and the corresponding keystrokes or mouse operations (Windows versions).&lt;br /&gt;&lt;br /&gt;&lt;table class="tablestyle" border="1" cols="2" width="90%"&gt;              &lt;tbody&gt; &lt;tr class="tablestyle2" bgcolor="#e4fafc"&gt;        &lt;td class="tablestyle"&gt;Actions&lt;/td&gt;        &lt;td class="tablestyle"&gt;Keystokes or Mouse Actions&lt;/td&gt;         &lt;/tr&gt;        &lt;tr class="tablestyle" align="left" valign="top"&gt;  &lt;td class="tablestyle"&gt;Copying a block from a library&lt;/td&gt;  &lt;td class="tablestyle"&gt;Drag the block to the model     window with the left button on the mouse OR use select the COPY and      PASTE from EDIT menu.&lt;/td&gt;       &lt;/tr&gt;       &lt;tr class="tablestyle" align="left" valign="top"&gt;       &lt;td class="tablestyle"&gt;Duplicating blocks in a model&lt;/td&gt;       &lt;td class="tablestyle"&gt;Hold down the CTRL key and select  the block with the left mouse drag the block to a new location.&lt;/td&gt;  &lt;/tr&gt; &lt;tr class="tablestyle" align="left" valign="top"&gt; &lt;td class="tablestyle"&gt;&lt;div align="left"&gt;Display block's parameters&lt;/div&gt;&lt;/td&gt; &lt;td class="tablestyle"&gt;&lt;div align="left"&gt;Double click on the block&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;  &lt;tr class="tablestyle"&gt; &lt;td class="tablestyle"&gt;Flip a block&lt;/td&gt;  &lt;td class="tablestyle"&gt;CTRL-F&lt;/td&gt;      &lt;/tr&gt;  &lt;tr class="tablestyle"&gt;  &lt;td class="tablestyle"&gt;&lt;div align="left"&gt;Rotate a block (clockwise 90 deg     @ each keystroke)&lt;/div&gt;&lt;/td&gt;  &lt;td class="tablestyle"&gt;CTRL-R&lt;/td&gt; &lt;/tr&gt; &lt;tr class="tablestyle" align="left" valign="top"&gt; &lt;td class="tablestyle"&gt;Changing blocks' names&lt;/td&gt; &lt;td class="tablestyle"&gt;&lt;div align="left"&gt;Click on block's label and     position the cursor to desired place.&lt;/div&gt;&lt;/td&gt;     &lt;/tr&gt;     &lt;tr class="tablestyle" align="left" valign="top"&gt;     &lt;td class="tablestyle"&gt;Disconnecting a block&lt;/td&gt;    &lt;td class="tablestyle"&gt;&lt;div align="left"&gt;hold down the SHIFT key and drag   the block to a new location&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class="tablestyle" align="left" valign="top"&gt;  &lt;td class="textstyle"&gt;&lt;div class="tablestyle" align="left"&gt;Drawing a diagonal line&lt;/div&gt;&lt;/td&gt; &lt;td class="tablestyle"&gt;hold down the SHIFT key while  dragging the mouse with the left button&lt;/td&gt;              &lt;/tr&gt;                  &lt;tr class="tablestyle" align="left" valign="top"&gt;                  &lt;td class="tablestyle"&gt;Dividing a line&lt;/td&gt;           &lt;td class="tablestyle"&gt;move the cursor to the line to    where you want to create the vertex and use the left button on the mouse   to drag the line while holding down the SHIFT key&lt;/td&gt;                 &lt;/tr&gt;             &lt;/tbody&gt;  &lt;/table&gt;                      &lt;p style="color: rgb(0, 0, 0);" class="textstyle"&gt;&lt;a name="annotation"&gt;&lt;/a&gt;&lt;b&gt;&lt;span style=""&gt;Annotations&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p style="color: rgb(0, 0, 0);" class="textstyle"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;To add an annotation to your model, place the cursor at an unoccupied area in your model window and double click (left button). A small rectangular area will appear with a cursor prompting for your input.&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(0, 0, 0);" class="textstyle"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;To delete an annotation, hold down the SHIFT key while selecting the annotation, then press the DELETE or BACKSPACE key. You may also change font type and colour from  FORMAT menu. Make sure that the block is selected before making the change.&lt;/span&gt;&lt;b&gt;&lt;span style=""&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CzN3cg4BCxs/SYsrzwBC64I/AAAAAAAAAIY/2jfWLYEEa9U/s1600-h/changfont.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 250px; height: 85px;" src="http://3.bp.blogspot.com/_CzN3cg4BCxs/SYsrzwBC64I/AAAAAAAAAIY/2jfWLYEEa9U/s400/changfont.jpg" alt="" id="BLOGGER_PHOTO_ID_5299377554594130818" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Hope this useful. Thanks for visiting.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5976912739175151841-9083425247071216181?l=programmingmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/9083425247071216181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5976912739175151841&amp;postID=9083425247071216181' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/9083425247071216181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/9083425247071216181'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/2009/02/simulink-introduction.html' title='Simulink Introduction'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_CzN3cg4BCxs/SYsnbvNqtRI/AAAAAAAAAHY/RMvlRKoHPk8/s72-c/simulinkicon.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5976912739175151841.post-3774115208373276925</id><published>2009-02-03T04:05:00.001-08:00</published><updated>2009-02-03T08:05:12.899-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='matlab basic'/><category scheme='http://www.blogger.com/atom/ns#' term='matlab'/><title type='text'>Introduction to Matlab</title><content type='html'>Matlab is both a powerful computational environment and a programming language that easily handles matrix and complex arithmetic.  It is a large software package that has many advanced features built-in, and it has become a standard tool for many working in science or engineering disciplines.  Among other things, it allows easy plotting in both two and three dimensions. &lt;p&gt;Matlab has two different methods for executing commands:  &lt;i&gt;interactive mode &lt;/i&gt;and &lt;i&gt;batch mode&lt;/i&gt;.  In interactive mode, commands are typed (or cut-and-pasted) into the 'command window'.  In batch mode, a series of commands are saved in a text file (either using Matlab's built-in editor, or another text editor such as Emacs) with a '.m' extension.  The batch commands in a file are then executed by typing the name of the file at the Matlab command prompt.  The advantage to using a '.m' file is that you can make small changes to your code (even in different Matlab sessions) without having to remember and retype the entire set of commands.  Also, when using Matlab's built-in editor, there are simple debugging tools that can come in handy when your programs start getting large and complicated.  More on writing .m files later.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:100%;"&gt;&lt;b&gt;Scalar Variables and Arithmetic Operators&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p&gt;Scalar variables are assigned in the obvious way: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x = 7&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;x =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;       7&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;The&lt;tt&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&gt;&gt;&lt;/span&gt; &lt;/tt&gt;is Matlab's command prompt.  Notice that Matlab echos back to you the value of the assignment you have made.  This can be helpful occasionally, but will generally be unwieldy when working with vectors.  By placing a semicolon at the end of a statement, you can suppress this verbose behavior. &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x = 7;&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Variables in Matlab follow the usual naming conventions.  Any combination of letters, numbers and underscore symbols ('_') can be used, as long as the first character is a letter.  Note also that variable names are case sensitive ('&lt;tt&gt;X&lt;/tt&gt;' is different from '&lt;tt&gt;x&lt;/tt&gt;'). &lt;/p&gt;&lt;p&gt;All of the expected scalar arithmetic operators are available: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; 2*x&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    14&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x^2&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    49&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Notice that the multiply operation is not implied as it is in some other computational environments and the '*' operator has to be specified (typing '&lt;tt&gt;&gt;&gt;2x&lt;/tt&gt;' will result in an error).  This is also a good time to point out that Matlab remembers the commands that you are entering and you can use the up-arrow button to scroll through them and edit them for re-entry.  This is very handy, especially when you are repeatedly making minor changes to a long command line. &lt;/p&gt;&lt;p&gt;There are a few predefined variables in Matlab that you will use often:  &lt;tt&gt;pi&lt;/tt&gt;, &lt;tt&gt;i&lt;/tt&gt;, and &lt;tt&gt;j&lt;/tt&gt;.   Both &lt;tt&gt;i&lt;/tt&gt; and &lt;tt&gt;j&lt;/tt&gt; are defined to be the square-root of -1 and &lt;tt&gt;pi&lt;/tt&gt; is defined as the usual 3.1416... .  These variables can be assigned other values, so the statement &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; pi = 4;&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;is valid.  Care is needed to avoid changing a predefined variable and then forgetting about that change later.  That may seem unreasonable with &lt;tt&gt;pi&lt;/tt&gt;, but &lt;tt&gt;i&lt;/tt&gt; and &lt;tt&gt;j&lt;/tt&gt; are both natural variables to use as indices and they are often changed.  A useful command is the '&lt;tt&gt;clear&lt;/tt&gt;', or '&lt;tt&gt;clear all&lt;/tt&gt;' command.  Typing either of these at the command prompt will remove all current variables from Matlab's memory and reset predefined variables to their original values.  The &lt;tt&gt;clear&lt;/tt&gt; command can also remove one specific variable from memory.  The command &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; clear x&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;would only remove the variable named '&lt;tt&gt;x&lt;/tt&gt;' from Matlab's memory. &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:100%;"&gt;&lt;b&gt;Matrix Variables and Arithmetic Operators&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p&gt;Another useful Matlab command is '&lt;tt&gt;whos&lt;/tt&gt;', which will report the names and dimensions of all variables currently in Matlab's memory.  After typing the command at the prompt we see: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; whos&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;  Name      Size         Bytes  Class&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;  x         1x1              8  double array&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;Grand total is 1 elements using 8 bytes&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;The important thing to note right now is that the size is given as '&lt;tt&gt;1x1&lt;/tt&gt;'.  Matlab is really designed to work with vectors and matrices, and a scalar variable is just a special case of a 1x1 dimensional vector.  To assign a vector containing the first 5 integers to the variable &lt;tt&gt;x&lt;/tt&gt;, we could type this command: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x = [1 2 3 2^2 2*3-1]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;x =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     1     2     3     4     5&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;We won't have much occasion to operate on matrices that are higher dimension, but if you wanted to create a 2-D matrix you could use a command something like: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; A = [1 2 3; 4 5 6]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;A =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     1     2     3&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     4     5     6&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;To create larger vectors than the toy examples above (say, the integers up to 100), we would need to type a lot of numbers.  Not surprisingly, there are easier ways built in to create vectors.  To create the same 5-element vector we did above, we could also type: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x = [1:5]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;x =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     1     2     3     4     5&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;The colon operator creates vectors of equally spaced elements given a beginning point, and maximum ending point and the step size in between elements.  Specifically, &lt;tt&gt;[b:s:e]&lt;/tt&gt; creates the vector &lt;tt&gt;[b b+s b+2*s ... e]&lt;/tt&gt;.  If no step size is specified (as in the example above), a step of 1 is assumed.  So, the command &lt;tt&gt;[1:2:10]&lt;/tt&gt; would create the vector of odd integers less than 10, &lt;tt&gt;[1 3 5 7 9]&lt;/tt&gt; and  the command &lt;tt&gt;[1:3:10]&lt;/tt&gt; would create the vector of elements &lt;tt&gt;[1 4 7 10]&lt;/tt&gt;. &lt;/p&gt;&lt;p&gt;Let's create the vector of odd elements mentioned above: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x_odd = [1:2:10]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;x_odd =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     1     3     5     7     9&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;You can access any  element by indexing the vector name with parenthesis (indexing starts from one, not from zero as in many other programming languages).  For instance,  to access the 3rd element, we would type: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x_odd(3)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     5&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;We can also access a range of elements (a subset of the original vector) by using the colon operator and giving a starting and ending index. &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; x_odd(2:4)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     3     5     7&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;If we want to do simple arithmetic on a vector and a scalar, the expected things happen, &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; 3+[1 2 3]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     4     5     6&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; 3*[1 2 3]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     3     6     9&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;and the addition or subtraction of matrices is possible as long as they are the same size: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; [1 2 3]+[4 5 6]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     5     7     9&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;The operators '&lt;tt&gt;*&lt;/tt&gt;' and '&lt;tt&gt;/&lt;/tt&gt;' actually represent matrix multiplication and division which is not typically what we will need in this course.  However, a common task will be to form a new vector by multiplying (or dividing) the elements of two vectors together, and the special operators '&lt;tt&gt;.*&lt;/tt&gt;' and '&lt;tt&gt;./&lt;/tt&gt;' serve that purpose. &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; [1 2 3].*[4 5 6]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;     4    10    18&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; [1 2 3]./[4 5 6]&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    0.2500    0.4000    0.5000&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Beware that the operator '&lt;tt&gt;^&lt;/tt&gt;' is a shortcut for repeated &lt;i&gt;matrix&lt;/i&gt; multiplications ('&lt;tt&gt;*&lt;/tt&gt;'), whereas the operator '&lt;tt&gt;.^&lt;/tt&gt;' is the shortcut for repeated &lt;i&gt;element-by-element&lt;/i&gt; multiplications ('&lt;tt&gt;.*&lt;/tt&gt;').  So, to square all of the elements in a vector, we would use &lt;/p&gt;&lt;tt style="color: rgb(51, 102, 255);"&gt;&gt;&gt; [1 2 3].^2&lt;/tt&gt;  &lt;tt style="color: rgb(51, 102, 255);"&gt;ans =&lt;/tt&gt;  &lt;tt style="color: rgb(51, 102, 255);"&gt;     1     4     9&lt;/tt&gt; &lt;p&gt;&lt;span style="font-size:100%;"&gt;&lt;b&gt;Built-in Commands&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p&gt;Matlab has many built-in commands to do both elementary mathematical operations and also complex scientific calculations.  The '&lt;tt&gt;help&lt;/tt&gt;' command will be useful in learning about built-in commands.  By typing &lt;tt&gt;help&lt;/tt&gt; at the command prompt, you are given a list of the different categories that Matlab commands fall into (e.g., general, elementary matrix operations, elementary math functions, graphics, etc.).  Notice that there are probably even specific toolboxes included with your Matlab package for performing computations for disciplines such as signal processing.  To see all of the commands under a certain topic, type '&lt;tt&gt;help topic&lt;/tt&gt;'.  To get a description of a specific command, type '&lt;tt&gt;help command&lt;/tt&gt;'. &lt;/p&gt;&lt;p&gt;We'll look at a couple of example commands.  First, the square-root command, &lt;tt&gt;sqrt&lt;/tt&gt;.  To calculate the square root of a number, type: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; sqrt(2)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    1.4142&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Again, to illustrate that Matlab understands complex numbers, we can have it calculate the root of a negative number: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; sqrt(-9)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;        0 + 3.0000i&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Many Matlab commands that operate on scalars will also operate element-by-element if you give it a vector.  For instance: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; sqrt([1 2 4 6])&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    1.0000    1.4142    2.0000    2.4495&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; sqrt([1:8])&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    1.0000    1.4142    1.7321    2.0000    2.2361    2.4495    2.6458    2.8284&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Another useful command is &lt;tt&gt;sin&lt;/tt&gt; function, which also operates on a vector. &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; sin([pi/4 pi/2 pi])&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ans =&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;    0.7071    1.0000    0.0000&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;It is important to realize that digital signals are really just a collection of points and can be represented by a vector.  Because Matlab allows functions like &lt;tt&gt;sin&lt;/tt&gt; and &lt;tt&gt;sqrt&lt;/tt&gt; (as well as many others) to operate on vectors, many of the signal definitions and calculation we would like to perform are very straight-forward.  We will illustrate this a little more explicitly in the next section.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:100%;"&gt;&lt;b&gt;Signals, Plotting and Batch Operation&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p&gt;We will now use the elementary tools in Matlab to build a basic signal we have talked about in class.  All of the commands given in this section are already written in the file &lt;i&gt;&lt;a href="http://www.owlnet.rice.edu/%7Eelec241/sineplot.m"&gt;sinplot.m&lt;/a&gt;&lt;/i&gt;.  They can be executed in batch by typing &lt;i&gt;sinplot&lt;/i&gt; at the command prompt.  If the file is not found, use the 'current directory' browser at the top of the Matlab window to make the current directory the one where the file lives. &lt;/p&gt;&lt;p&gt;First, we will define a signal which is a 2 Hz sinewave over the interval [0,1] seconds: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; t   = [0:.01:1];                % independent (time) variable&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; A = 8;                          % amplitude&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; f_1 = 2;                        % create a 2 Hz sine wave lasting 1 sec&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; s_1 = A*sin(2*pi*f_1*t);&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;Anything following a '&lt;tt&gt;%&lt;/tt&gt;' symbol is a comment and will be ignored by Matlab.  A 4 Hz sinewave with the same amplitude will also be defined: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; f_2 = 4;                        % create a 4 Hz sine wave lasting 1 sec&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; s_2 = A*sin(2*pi*f_2*t);&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;There are a few commands necessary to do basic plotting of these signals.  The &lt;tt&gt;figure&lt;/tt&gt; command will bring up a new figure window and the &lt;tt&gt;close&lt;/tt&gt; command closes a specific window ('&lt;tt&gt;close all&lt;/tt&gt;' closes all open windows).  The &lt;tt&gt;subplot(r, c, p)&lt;/tt&gt; command allows many plots to be 'tiled' into &lt;tt&gt;r&lt;/tt&gt; rows and &lt;tt&gt;c&lt;/tt&gt; columns on one figure window.  After this command is issued, any succeeding commands will affect the &lt;tt&gt;p&lt;/tt&gt;&lt;sup&gt;th&lt;/sup&gt; tile.  The &lt;tt&gt;plot(x,y)&lt;/tt&gt; command will then plot the equal-length vectors &lt;tt&gt;x&lt;/tt&gt; and &lt;tt&gt;y&lt;/tt&gt; on the appropriate axis and in the obvious way.  The two sinewaves created above are then plotted along with the sum of the two signals in separate tiles with the commands: &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;%plot the 2 Hz sine wave in the top panel&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;figure&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;subplot(3,1,1)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;plot(t, s_1)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;title('2 Hz sine wave')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ylabel('Amplitude')&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;%plot the 4 Hz sine wave in the middle panel&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;subplot(3,1,2)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;plot(t, s_2)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;title('4 Hz sine wave')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ylabel('Amplitude')&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;%plot the summed sine waves in the bottom panel&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;subplot(3,1,3)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;plot(t, s_1+s_2)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;title('Summed sine waves')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ylabel('Amplitude')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;xlabel('Time (s)')&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;The commands &lt;tt&gt;title&lt;/tt&gt;, &lt;tt&gt;xlabel&lt;/tt&gt;, and &lt;tt&gt;ylabel&lt;/tt&gt;  all take strings as arguments and apply the appropriate label to the top, x-axis or y-axis of the current subplot, respectively.&lt;br /&gt;&lt;/p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYhnFVw99TI/AAAAAAAAAGg/x7NSVLl7US0/s1600-h/sine1.gif"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 397px; height: 322px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYhnFVw99TI/AAAAAAAAAGg/x7NSVLl7US0/s400/sine1.gif" alt="" id="BLOGGER_PHOTO_ID_5298598303040927026" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Normally, whenever you have plotted a function on a subplot and you try to plot another function, Matlab will erase the subplot and start over.  Often you will want to plot two signals on top of each other for comparison.  This can be done by using the '&lt;tt&gt;hold&lt;/tt&gt;', which toggles the hold property of a subplot on or off.  In this next example, the 2 original sinewaves are created again, this time using the built-in exponential function &lt;tt&gt;exp&lt;/tt&gt;, the built-in function &lt;tt&gt;imag&lt;/tt&gt; and Euler's relation.  The &lt;tt&gt;imag&lt;/tt&gt; function just returns the imaginary part of a complex number.  These statements accomplish the same thing that the &lt;tt&gt;sin&lt;/tt&gt; function did earlier, and are only used to illustrate again that Matlab handles all of the complex arithmetic we need very naturally.  In this example, the two sinewaves are plotted on top of each other, and the sum is plotted again in its own subplot.  Notice that the &lt;tt&gt;plot&lt;/tt&gt; command can also take a third argument that specifies the color and linestyle of the resulting plot.  There are many options here, and they can be viewed by typing &lt;tt&gt;help plot&lt;/tt&gt; at the prompt. &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;% create the same sine waves using Euler's relations&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;s_3 = (exp(j*2*pi*f_1*t) - exp(-j*2*pi*f_1*t))/(2*j);&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;s_4 = imag(exp(j*2*pi*f_2*t));&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;% plot the 2 and 4 Hz waves together in the same panel&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;figure&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;subplot(2,1,1)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;plot(t,s_3, 'b-')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;hold on&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;plot(t, s_4, 'r--')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ylabel('Amplitude')&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;% again plot the sum in the bottom panel&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;subplot(2,1,2)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;plot(t, s_3+s_4)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;ylabel('Amplitude')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;xlabel('Time (s)')&lt;/tt&gt;&lt;br /&gt;&lt;/p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYhnm5A3QmI/AAAAAAAAAGo/AUDBXCZ0coo/s1600-h/sine2.gif"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 363px; height: 300px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYhnm5A3QmI/AAAAAAAAAGo/AUDBXCZ0coo/s400/sine2.gif" alt="" id="BLOGGER_PHOTO_ID_5298598879438520930" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;b&gt;Basic Programming (For Loops)&lt;/b&gt;&lt;/span&gt; &lt;p&gt;Though we haven't mentioned much of this, Matlab is also actually a programming language with all of the usual iterative and conditional execution abilities.  Because it is matrix based, many operations that you would normally use a loop for (say, if you were programming in C) can be done much faster using Matlab's built-in matrix operations.  But, loops are sometimes unavoidable when you have repeated operations to perform on a list of parameters, and we will illustrate a simple one with an example here.  All of the commands given in this section can be found in the file &lt;i&gt;&lt;a href="http://www.owlnet.rice.edu/%7Eelec241/sumsineplot.m"&gt;sumsinplot.m&lt;/a&gt;&lt;/i&gt;, and can be executed in batch by typing &lt;i&gt;sumsineplot&lt;/i&gt; at the command prompt. &lt;/p&gt;&lt;p&gt;By far, the most common looping apparatus (in Matlab, at least) is the &lt;tt&gt;for&lt;/tt&gt; loop.  The basic  &lt;tt&gt;for&lt;/tt&gt; loop has the following structure: &lt;/p&gt;&lt;p&gt;&lt;tt&gt;for&lt;/tt&gt; &lt;i&gt;variable&lt;/i&gt; &lt;tt&gt;=&lt;/tt&gt; &lt;i&gt;values&lt;/i&gt;&lt;br /&gt;&lt;i&gt;statements&lt;/i&gt;&lt;br /&gt;&lt;tt&gt;end&lt;/tt&gt; &lt;/p&gt;&lt;p&gt;where &lt;i&gt;variable&lt;/i&gt; in this case is generally some sort of counter or indexing variable.  This is probably best explained through an illustration.  In this example, we will generate sinwaves with odd integers for frequencies (1, 3, 5, 7, 9, 11) and cumulatively sum them together. After each new sinewave is added to the others, the result is plotted in its own subplot.  To be explicit, a 1 Hz sinewave is generated and plotted.  Then a 3 Hz sinewave is generated, added to the 1 Hz sinewave and plotted.  A 5 Hz sinewave is generated, added to the sum of the 1st two signals and plotted, etc.  The only new things in this example are the use of the &lt;tt&gt;for&lt;/tt&gt; loop, and the use of the zeros command.  The &lt;tt&gt;zeros(n,m)&lt;/tt&gt; command returns a vector of dimension &lt;tt&gt;(n x m)&lt;/tt&gt;, initialized to all zeros. &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;%plot cumulatively summed sine waves with odd integer frequencies scaled by 1/f&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; t = 0:.001:1;        % independent (time) variable&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; A = 1;              % amplitude&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; f = 1:2:11;         % odd frequencies&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; s = zeros(1,101);   % empty 'signal' vector to start with&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; figure&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;% for each frequency, create the signal and add it into s(t)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; for count=1:6&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt;     s = s + A*sin(2*pi*f(count)*t)/f(count);&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt;     subplot(6,1,count)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt;     plot(t,s);&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt;     ylabel('Amplitude')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; end&lt;/tt&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 102, 255);"&gt;&lt;tt&gt;&gt;&gt; subplot(6,1,1)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; title('Cumulatively summing sine waves')&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; subplot(6,1,6)&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&gt;&gt; xlabel('Time (s)')&lt;/tt&gt;&lt;br /&gt;&lt;/p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYhoVmf_3vI/AAAAAAAAAGw/fh47uUg7vSs/s1600-h/sumsine.gif"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 400px; height: 300px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYhoVmf_3vI/AAAAAAAAAGw/fh47uUg7vSs/s400/sumsine.gif" alt="" id="BLOGGER_PHOTO_ID_5298599681922686706" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5976912739175151841-3774115208373276925?l=programmingmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/3774115208373276925/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5976912739175151841&amp;postID=3774115208373276925' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/3774115208373276925'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/3774115208373276925'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/2009/02/introduction-to-matlab.html' title='Introduction to Matlab'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_CzN3cg4BCxs/SYhnFVw99TI/AAAAAAAAAGg/x7NSVLl7US0/s72-c/sine1.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5976912739175151841.post-5117114853914247411</id><published>2009-02-03T02:36:00.000-08:00</published><updated>2009-02-03T03:41:12.962-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='image processing'/><category scheme='http://www.blogger.com/atom/ns#' term='matlab'/><title type='text'>Imfilter function for Nonlinear Operation</title><content type='html'>Some nonlinear image processing operations can be expressed in terms of linear filtering. When this is true, it often provides a recipe for a speedy MATLAB implementation. Today I'll show two examples: Computing the local standard deviation, and computing the local geometric mean.&lt;br /&gt;&lt;br /&gt;The local standard deviation operator is often used as a measure of "busy-ness" throughout the image.  For each pixel, the       standard deviation of that pixel's neighbors is computed:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;sqrt(sum((x - xbar).^2));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Mathematically, the summation can be rewritten as:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;sum(x.^2) - sum(x).^2/N&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Both of these local sums can be computed by using &lt;a href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/imfilter.html"&gt;&lt;tt&gt;imfilter&lt;/tt&gt;&lt;/a&gt; with an all-ones filter.&lt;br /&gt;&lt;pre  style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:courier new;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;I = im2double(imread(&lt;/span&gt;&lt;span style="color: rgb(160, 32, 240);"&gt;'cameraman.tif'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;));&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;imshow(I);&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYggro3NXbI/AAAAAAAAAFw/e-v8OJCDV5U/s1600-h/camera1.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYggro3NXbI/AAAAAAAAAFw/e-v8OJCDV5U/s400/camera1.jpg" alt="" id="BLOGGER_PHOTO_ID_5298520895676833202" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To compute the sum(x.^2) term, we square (elementwise) the input to &lt;tt style="color: rgb(51, 102, 255);"&gt;imfilter&lt;/tt&gt;.&lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(0, 0, 0);"&gt;h = ones(5,5);&lt;br /&gt;term1 = imfilter(I.^2, h);&lt;br /&gt;imshow(term1, [])&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYghTWKE_2I/AAAAAAAAAF4/Mx3A-74n6Kc/s1600-h/camera2.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYghTWKE_2I/AAAAAAAAAF4/Mx3A-74n6Kc/s400/camera2.jpg" alt="" id="BLOGGER_PHOTO_ID_5298521577850470242" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Notice the dark band around the edge.  This is because &lt;tt style="color: rgb(51, 102, 255);"&gt;imfilter&lt;/tt&gt; zero-pads by default.  We might want to use the 'symmetric' option instead.&lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;term1 = imfilter(I.^2, h, &lt;span style="color: rgb(160, 32, 240);"&gt;'symmetric'&lt;/span&gt;);&lt;br /&gt;imshow(term1, [])&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYgi7E6Fb6I/AAAAAAAAAGA/j3w2EW9j2eQ/s1600-h/camera3.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SYgi7E6Fb6I/AAAAAAAAAGA/j3w2EW9j2eQ/s400/camera3.jpg" alt="" id="BLOGGER_PHOTO_ID_5298523359926382498" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To compute the sum(x).^2 term, we square the output of &lt;tt style="color: rgb(51, 102, 255);"&gt;imfilter&lt;/tt&gt;.&lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;term2 = imfilter(I, h, &lt;span style="color: rgb(160, 32, 240);"&gt;'symmetric'&lt;/span&gt;).^2 / numel(h);&lt;br /&gt;imshow(term2, [])&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYgjwSQeg3I/AAAAAAAAAGI/Vdac0LUH2c4/s1600-h/camera4.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SYgjwSQeg3I/AAAAAAAAAGI/Vdac0LUH2c4/s400/camera4.jpg" alt="" id="BLOGGER_PHOTO_ID_5298524274043028338" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Then we subtract the second term from the first and take the square root.&lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;local_std = sqrt(term1 - term2);  &lt;span style="color: rgb(34, 139, 34);"&gt;% scale factor omitted&lt;/span&gt;&lt;br /&gt;imshow(local_std, [])&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CzN3cg4BCxs/SYglbaFvv6I/AAAAAAAAAGQ/IdDlWKB3w-k/s1600-h/camera5.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://3.bp.blogspot.com/_CzN3cg4BCxs/SYglbaFvv6I/AAAAAAAAAGQ/IdDlWKB3w-k/s400/camera5.jpg" alt="" id="BLOGGER_PHOTO_ID_5298526114391506850" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Cautionary Notes&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1. The procedure shown here is not always a numerically sound way to compute the standard deviation, because it can suffer from both overflow (squared terms) and underflow (cancellation in the subtraction of large numbers). For typical image pixel values and window sizes, though, it works reasonably well.&lt;br /&gt;&lt;br /&gt;2. Round-off error in the computation of term1 and term2 can sometimes make (term1 - term2) go slightly negative, resulting in             complex outputs from square root operator.  Avoid this problem by using this code:    &lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;local_std = sqrt(max(term1 - term2, 0));&lt;/pre&gt;Recent releases of the Image Processing Toolbox include the function &lt;a href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/stdfilt.html"&gt;&lt;tt&gt;stdfilt&lt;/tt&gt;&lt;/a&gt;, which does all this work for you.&lt;br /&gt;The local geometric mean filter multiplies together all the pixel values in the neighborhood and then takes the N-th root, where N is the number of pixels in the neighborhood. The geometric mean filter is said to be slightly better than the arithmetic mean at preserving image detail.&lt;br /&gt;Use the old logarithm trick to express the geometric mean in terms of a summation.  Then &lt;tt style="color: rgb(51, 102, 255);"&gt;imfilter&lt;/tt&gt; can be used to compute the neighborhood summation, like this.&lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;geo_mean = imfilter(log(I), h, &lt;span style="color: rgb(160, 32, 240);"&gt;'replicate'&lt;/span&gt;);&lt;br /&gt;geo_mean = exp(geo_mean);&lt;br /&gt;geo_mean = geo_mean .^ (1/numel(h));&lt;br /&gt;&lt;br /&gt;imshow(geo_mean, [])&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYgnQz57VCI/AAAAAAAAAGY/-OUnXqsW_KE/s1600-h/camera6.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SYgnQz57VCI/AAAAAAAAAGY/-OUnXqsW_KE/s400/camera6.jpg" alt="" id="BLOGGER_PHOTO_ID_5298528131365950498" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is the complete source code. You can directly copy and paste to an M-File and run it.&lt;br /&gt;&lt;pre style="border: 1px solid rgb(200, 200, 200); padding: 10px; background: rgb(249, 247, 243) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;br /&gt;%% Nonlinear filtering using imfilter&lt;br /&gt;% Some nonlinear image processing operations can be&lt;br /&gt;% expressed in terms of linear filtering. &lt;br /&gt;% When this is true, it often provides a recipe&lt;br /&gt;% for a speedy MATLAB implementation. Today I'll&lt;br /&gt;% show two examples:&lt;br /&gt;% Computing the local standard deviation, and&lt;br /&gt;% computing the local geometric mean.&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% The local standard deviation operator&lt;br /&gt;% is often used as a measure of "busy-ness"&lt;br /&gt;% throughout the image.  For each pixel,&lt;br /&gt;% the standard deviation of that&lt;br /&gt;% pixel's neighbors is computed:&lt;br /&gt;%&lt;br /&gt;% sqrt( sum ( (x - xbar).^2 ) )&lt;br /&gt;%&lt;br /&gt;% (Scale factors omitted.)&lt;br /&gt;%&lt;br /&gt;% Mathematically, the summation can be&lt;br /&gt;% rewritten as:&lt;br /&gt;%&lt;br /&gt;% sum(x.^2) - sum(x).^2/N&lt;br /&gt;%&lt;br /&gt;% Both of these local sums can be computed&lt;br /&gt;% by using &lt;http: com="" access="" helpdesk="" help="" toolbox="" images="" html=""&gt;with an all-ones filter.&lt;br /&gt;&lt;br /&gt;I = im2double(imread('cameraman.tif'));&lt;br /&gt;imshow(I)&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% _Original image credit:&lt;br /&gt;% Massachusetts Institute of Technology_&lt;br /&gt;%&lt;br /&gt;% To compute the sum(x.^2) term, we square&lt;br /&gt;% (elementwise) the input to |imfilter|.&lt;br /&gt;&lt;br /&gt;h = ones(5,5);&lt;br /&gt;term1 = imfilter(I.^2, h);&lt;br /&gt;imshow(term1, [])&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% Notice the dark band around the edge. &lt;br /&gt;% This is because |imfilter| zero-pads&lt;br /&gt;% by default.  We might want to use the&lt;br /&gt;% 'symmetric'option instead.&lt;br /&gt;&lt;br /&gt;term1 = imfilter(I.^2, h, 'symmetric');&lt;br /&gt;imshow(term1, [])&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% To compute the sum(x).^2 term, we square&lt;br /&gt;% the output of |imfilter|.&lt;br /&gt;&lt;br /&gt;term2 = imfilter(I,h,'symmetric').^2/numel(h);&lt;br /&gt;imshow(term2, [])&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% Then we subtract the second term from&lt;br /&gt;% the first and take the square root.&lt;br /&gt;&lt;br /&gt;local_std = sqrt(term1-term2); % scale factor omitted&lt;br /&gt;imshow(local_std, [])&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% _Cautionary notes_&lt;br /&gt;%&lt;br /&gt;% * The procedure shown here is not always a numerically&lt;br /&gt;% sound way to compute the standard deviation,&lt;br /&gt;% because it can suffer from both overflow&lt;br /&gt;% (squared terms) and underflow (cancellation in the&lt;br /&gt;% subtraction of large numbers). For typical image pixel&lt;br /&gt;% values and window sizes, though, it works reasonably&lt;br /&gt;% well.&lt;br /&gt;%&lt;br /&gt;% * Round-off error in the computation of term1 and&lt;br /&gt;% term2 can sometimes make (term1 - term2) go slightly&lt;br /&gt;% negative, resulting in complex outputs from square root&lt;br /&gt;% operator. Avoid this problem by using this code:&lt;br /&gt;&lt;br /&gt;local_std = sqrt(max(term1 - term2, 0));&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% Recent releases of the Image Processing Toolbox&lt;br /&gt;% include the function&lt;http: com="" access="" helpdesk="" help="" toolbox="" images="" html=""&gt; which does all this work for you.&lt;br /&gt;&lt;br /&gt;%%&lt;br /&gt;% The local geometric mean filter multiplies together&lt;br /&gt;% all the pixel values in the neighborhood&lt;br /&gt;% and then takes the N-th root, where N is the number&lt;br /&gt;% of pixels in the neighborhood. The geometric mean&lt;br /&gt;% filter is said to be slightly better than&lt;br /&gt;% the arithmetic mean at preserving image detail.&lt;br /&gt;%&lt;br /&gt;% Use the old logarithm trick to express the geometric&lt;br /&gt;% mean in terms of a summation.  Then |imfilter| can be&lt;br /&gt;% used to compute the neighborhood summation, like this.&lt;br /&gt;&lt;br /&gt;geo_mean = imfilter(log(I), h, 'replicate');&lt;br /&gt;geo_mean = exp(geo_mean);&lt;br /&gt;geo_mean = geo_mean .^ (1/numel(h));&lt;br /&gt;&lt;br /&gt;imshow(geo_mean, [])&lt;br /&gt;&lt;br /&gt;&lt;/http:&gt;&lt;/http:&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="text-align: right;"&gt;&lt;span style="font-size:78%;"&gt;&lt;span style="font-style: italic;"&gt;Copyright 2008 Mathworks, Inc&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5976912739175151841-5117114853914247411?l=programmingmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/5117114853914247411/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5976912739175151841&amp;postID=5117114853914247411' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/5117114853914247411'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/5117114853914247411'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/2009/02/imfilter-function-for-nonlinear.html' title='Imfilter function for Nonlinear Operation'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_CzN3cg4BCxs/SYggro3NXbI/AAAAAAAAAFw/e-v8OJCDV5U/s72-c/camera1.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5976912739175151841.post-4223378715379011413</id><published>2008-11-07T08:08:00.000-08:00</published><updated>2008-11-08T06:11:01.322-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='serial port'/><category scheme='http://www.blogger.com/atom/ns#' term='matlab'/><title type='text'>How to Access Serial Port from Matlab</title><content type='html'>This part of programming is very useful for engineers who want to simulate their code that access serial port easily. It is even simple than we ever imagine. Only need 3 lines for declaration, set any properties and open it.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-size:85%;" &gt;&lt;span style="font-family:courier new;"&gt;s = serial('COM1');&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;set(s,'BaudRate',4800);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;fopen(s);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The first line "tells" the computer that we want to use com1 port, which must be available. As information, one computer could have more than one serial port, which means we can replace &lt;span style="font-style: italic;"&gt;com1 &lt;/span&gt;with &lt;span style="font-style: italic;"&gt;com2&lt;/span&gt; etc as long as the port is available. This command will create a serial port object &lt;span style="font-style: italic; color: rgb(51, 102, 255);"&gt;S&lt;/span&gt; in memory.&lt;br /&gt;The second line means we set the Baudrate of serial port to 4800. Baudrate is one of configurable properties of serial port object. Other properties that we can configure are :&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 102, 204);font-size:85%;" &gt;&lt;span style="font-family: courier new;"&gt;    &lt;span style="color: rgb(51, 102, 255);"&gt;ByteOrder: [ {littleEndian} | bigEndian ] &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    BytesAvailableFcn&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    BytesAvailableFcnCount&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    BytesAvailableFcnMode: [ {terminator} | byte ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    ErrorFcn&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    InputBufferSize&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    Name&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    OutputBufferSize&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    OutputEmptyFcn&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    RecordDetail: [ {compact} | verbose ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    RecordMode: [ {overwrite} | append | index ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    RecordName&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    Tag&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    Timeout&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    TimerFcn&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    TimerPeriod&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    UserData&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    &lt;/span&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;BaudRate&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    BreakInterruptFcn&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    DataBits&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    DataTerminalReady: [ {on} | off ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    FlowControl: [ {none} | hardware | software ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    Parity: [ {none} | odd | even | mark | space ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    PinStatusFcn&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    Port&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    ReadAsyncMode: [ {continuous} | manual ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    RequestToSend: [ {on} | off ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    StopBits&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);"&gt;    Terminator&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The third line means that we want to connect to the real serial port hardware described in the first line above.&lt;br /&gt;If you already have an indicator attached to your serial port (for example a led or a microcontroller)  that will give an action when we send the data to serial port, than you can start to send data to it. You cannot use Hyperterminal because there will be a crash in our application.&lt;br /&gt;Assumed that the device indicator already attached, than you can send data in a number or ascii format or binary :&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;font-size:85%;" &gt;&lt;span style="color: rgb(51, 102, 255);"&gt;fwrite(s,68)&lt;/span&gt;&lt;/span&gt;;&lt;br /&gt;or&lt;br /&gt;&lt;span style="font-family: courier new; color: rgb(51, 102, 255);font-size:85%;" &gt;fwrite(s,'D');&lt;/span&gt;&lt;br /&gt;or&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);font-size:85%;" &gt;&lt;span style="font-family: courier new;"&gt;fwrite(s,1000100);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;(to be continue ...)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5976912739175151841-4223378715379011413?l=programmingmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/4223378715379011413/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5976912739175151841&amp;postID=4223378715379011413' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/4223378715379011413'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/4223378715379011413'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/2008/11/how-to-access-serial-port-from-matlab.html' title='How to Access Serial Port from Matlab'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5976912739175151841.post-8370935424755687087</id><published>2008-11-06T08:17:00.000-08:00</published><updated>2008-11-07T07:56:17.094-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='gui'/><category scheme='http://www.blogger.com/atom/ns#' term='matlab'/><title type='text'>GUI (Graphical User Interface) Design on Matlab</title><content type='html'>It is very easy to design a user interface (commonly called a form) on Matlab. Although the GUI items/components (such us button, textbox, combobox, chart, etc) are not as much as Borland Delphi or Microsoft Visual Studio, it is very comfortable for a "newbie" programmer (or tobe) to use Matlab GUI for their first initial learning about graphical programming. Here, Matlab offers a minimalist concept for us to build an interactive application.&lt;br /&gt;&lt;div style="text-align: justify;"&gt;This session will give step-by-step tutorial on how to start building a simple GUI form within Matlab environment.&lt;br /&gt;&lt;br /&gt;1. As usually, run your Matlab.&lt;br /&gt;2. Then go to &lt;span style="font-style: italic;"&gt;File -&gt; New -&gt; GUI&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRRA4__SzI/AAAAAAAAADU/aB1jHqo8360/s1600-h/guimenu.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 327px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRRA4__SzI/AAAAAAAAADU/aB1jHqo8360/s400/guimenu.gif" alt="" id="BLOGGER_PHOTO_ID_5265922940045577010" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;3. GUIDE Quick Start window will appear. There are two selection tabs, &lt;span style="font-style: italic;"&gt;Create New GUI&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;Open Existing GUI&lt;/span&gt;. In this case, use &lt;span style="font-style: italic;"&gt;Create New GUI&lt;/span&gt;. Since we will create a new blank GUI, then select &lt;span style="font-style: italic;"&gt;Blank GUI (Default)&lt;/span&gt;. Click OK button.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_CzN3cg4BCxs/SRRYevpipEI/AAAAAAAAADc/2MDEw8zQZTU/s1600-h/duiwind.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 275px;" src="http://3.bp.blogspot.com/_CzN3cg4BCxs/SRRYevpipEI/AAAAAAAAADc/2MDEw8zQZTU/s400/duiwind.gif" alt="" id="BLOGGER_PHOTO_ID_5265931149512975426" border="0" /&gt;&lt;/a&gt;4. The new window with title "untitled1.fig" will be shown. This window contain a new form, and on this form we can put any GUI components.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRbKL3aheI/AAAAAAAAAD0/17XAV70FKXE/s1600-h/guiform.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 331px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRbKL3aheI/AAAAAAAAAD0/17XAV70FKXE/s400/guiform.gif" alt="" id="BLOGGER_PHOTO_ID_5265934094844986850" border="0" /&gt;&lt;/a&gt;5. From this point, we can run our new GUI application. Click &lt;span style="font-style: italic;"&gt;Run&lt;/span&gt; button. This Run button is on the toolbar, on the top part of window.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_CzN3cg4BCxs/SRReX4bE3gI/AAAAAAAAAEE/_YG22VJRilY/s1600-h/runbtn.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 232px; height: 82px;" src="http://2.bp.blogspot.com/_CzN3cg4BCxs/SRReX4bE3gI/AAAAAAAAAEE/_YG22VJRilY/s400/runbtn.gif" alt="" id="BLOGGER_PHOTO_ID_5265937628678905346" border="0" /&gt;&lt;/a&gt;6. The save confirmation dialog titled &lt;span style="font-style: italic;"&gt;GUIDE&lt;/span&gt; will appear. You can either thick "Do not show this dialog again" or leave it blank, then click &lt;span style="font-style: italic;"&gt;Yes &lt;/span&gt;button.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_CzN3cg4BCxs/SRRfL1_-rzI/AAAAAAAAAEM/4fPci_xwDpg/s1600-h/savec.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 374px; height: 165px;" src="http://4.bp.blogspot.com/_CzN3cg4BCxs/SRRfL1_-rzI/AAAAAAAAAEM/4fPci_xwDpg/s400/savec.gif" alt="" id="BLOGGER_PHOTO_ID_5265938521381580594" border="0" /&gt;&lt;/a&gt;7. After you give the name of your application and save it on the disk, the m-file editor related to this new application will be opened, and a few second later your application should be run.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRilCBGAnI/AAAAAAAAAEU/7G0-FDAEt2g/s1600-h/run.GIF"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 320px;" src="http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRilCBGAnI/AAAAAAAAAEU/7G0-FDAEt2g/s400/run.GIF" alt="" id="BLOGGER_PHOTO_ID_5265942252639093362" border="0" /&gt;&lt;/a&gt;8. That is it, now we already have a new GUI application which contain only one form. In Matlab environment, GUI application produces 2 files: a figure file with .fig extension and a m-file with .m extension. Just check inside your folder where you save your GUI application.&lt;br /&gt;&lt;br /&gt;Until this point, we do not write any code into our GUI application yet. We will have it on the next session of Matlab GUI Programming.&lt;br /&gt;Thank you for visiting.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5976912739175151841-8370935424755687087?l=programmingmatlab.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingmatlab.blogspot.com/feeds/8370935424755687087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5976912739175151841&amp;postID=8370935424755687087' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/8370935424755687087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5976912739175151841/posts/default/8370935424755687087'/><link rel='alternate' type='text/html' href='http://programmingmatlab.blogspot.com/2008/11/gui-graphical-user-interface-design-on.html' title='GUI (Graphical User Interface) Design on Matlab'/><author><name>virtualcore</name><uri>http://www.blogger.com/profile/06812553254416007079</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_CzN3cg4BCxs/SRRRA4__SzI/AAAAAAAAADU/aB1jHqo8360/s72-c/guimenu.gif' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
