Review Language Constructs and Work with Strings
Lab BA 113 - March 11, 1998

This URL is www.stewart.cs.sdsu.edu/cs205/module7/index.html

We've discussed the control of Output that comes from using the semicolon (;) at the end of MATLAB commands. Another means of output control of how your MATLAB scripts comes from the echo on and echo off commands. The default situation is to have MATLAB echo each command when you run a MATLAB script. This can lead to a large amount of information being placed on your screen. One way to control the scrolling of information on the screen is through the more command.

more on

will turn scrolling on so that you will see a screen full of text and then a prompt at the bottom of the screen

--more--

giving you time to read everything on the screen. When you are ready for the next screen, hit the space bar.

Have you had the chance to use the MATLAB diary command? I find this an effective mechanism to keep track of my output as well as some of my explorations.

diary explore3_5

creates a text file called explore3_5 in the directory you were in when you issued the matlab command to start MATLAB up. You can use a text editor (pico) to examine the file later as you explore different command combinations.

Another way to control this is to insert echo off in you MATLAB scripts. Then only explicit output command, such as disp and fprint will cause output to be written to your screen.

In Module 2, we began to look at Input/Output Module2/Apply. At this time we focussed on the disp command which will display a single data item, either a number or a string. We also examined the fprintf command which allowed a great deal of flexibility and control over the output, requiring the programmer to specify how easy data item is printed following the standard C format programming language conventions.

MATLAB is a vector oriented language. In the case of I/O, the data specifier will be applied to each element of the vector. Try this out using

x = rand(20,1)
x
fprintf('%10.6f\n',x)

In the example above, we set up some floating point numbers with the rand command and then have MATLAB echo the values in x out for us in the default notation. We follow this with the fprintf command to precisely control output.

Strings

Sometimes the programmer want to have pretty prompt messages. We'll use a subset of the Getting Started on line documentation on Other Data Structures which discusses Characters and Text. NOTE: some of these figures only look "good" on a Mac or PC - and not UNIX! You should try the specific commands within a MATLAB window

F = reshape(32:127,16,6)'; % printable characters in ASCII character set
char(F) % convert the integer back to characters with char

The reshape command takes the vector of integers 32,33,34...,127 and reshapes them into a 16 by 6 matrix. This will make a nice display for the char command.

char(F+128) % shift the numbers 32,33,34,...,127 by 128

to see the Symbol and Wingdings. These characters look best on a Mac or PC.

Strings are an interesting complement to working with numbers.

s = 'hello there';

y = 'hi, my name is mary and i''m six years old. my sister ...

is a nice person';

Note the use of the ... continuation. We can also glue strings together

mystring = [s y]

Run this example in a MATLAB window to see what is printed out.

Scripts, Functions and GLOBAL variables

In the MATLAB Getting Started section on Scripts and Functions there is another nice description of these powerful constructs which you used in your second programming assignment.

The key structure is given below - the reserved words of MATLAB, i.e. the words that must be used just so are in bold. Suppose you want to write a function with the name myfun with one output value output1 and two input values in1 and in2.

function output1 = myfun (in1,in2) Suppose you want to write a function with the name yourfun with two output values output1 and output2 and two input values in1 and in2

function [output1,output2] = myfun (in1,in2)

Note, when there are more than one output values, we must use the brackers [].

An important new concept presented in the document above concerns the MATLAB variables to be shared between functions, but not placed in the input parameter list. The is accomplished by placing the declaration

global GRAVITY
GRAVITY = 32;
y = falling((0:.1:5)');

allowing the user-defined function

function h = falling(t)
global GRAVITY
h = 1/2 * GRAVITY * t .^ 2;

Flow Control

The Online Documentation - "Getting Started" reviews Flow Control which we examine in Module 5: Programming, Logic, Loops, Selection Statement

Graphics

"Getting Started" review Graphics