MATLAB Hints and Tricks #0

12.747 Problem Set Format Instructions



Below are the instructions that you should use to format and hand in your Problem sets. In general, problem sets should be sent in via netscape mail as an attatched m-file; there should be 1 m-file per problem. M-files are executable program files; a proper answer will be an m-file that we can execute in matlab. As a result, the program will download the necessary data, print text, graphics using the many features of Matlab.

Starting Out

These initial steps include instructions on how to set up your file structure and start, Matlab, Netscape, and emacs (a text editor). The idea here is to write your m-file in emacs with matlab running. Each time you modify your emacs file, you can save it and then execute it in matlab to see what it does. With both windows open, you can go back and forth between emacs and Matlab alternately tweaking your program and then running it until it does exactly what you want it to do. This method will allow what I am sure will be many hours of totally uninterrupted programming fun and unsurpassed modeling pleasure!

Note: If you alredy know Matlab and want to use an alternate system to deliver your m-files to us, you may; The official system will be using your Athena accounts and running the resident Matlab, emacs, and Netscape programs which are available to you. If you decide to use another system (ie. Matlab for dos, Linux, or some other combination) the burden is on you to to make your system work and to deliver problem sets on-time that work with our system.

1. Login to Athena at the prompt, type filemgr and hit return.

athena% filemgr (return)

(The (return) simply means to hit the return key, sometimes labeled `enter'. When we say ``type return'' we really mean, ``Hit the return key'').

When an outline of a box appears hit return again and you should see the File manager appear. Right click on File and pull down to create a new folder. You should see a folder titled NewFolder in the lower window. Pull the mouse arrow to this window and type modclass and hit return. You should see the name of NewFolder change to modclass. Right click again on File and pull down to Quit File Manager. This should put you back at the athena prompt

2. Change directories from your home directory to the modclass directory by typing cd modclass at the athena prompt.

athena% cd modclass (return)

3. From the modclass directory start matlab by first typing add matlab at the athena prompt and then typing matlab & to start matlb.

athena% add matlab (return)
athena% matlab & (return)

When a window outline appears, hit return again. A Matlab graphics window will appear and then disappear, this is OK. Eventually, matlab will start and you will see a >> prompt in the Matlab window. You can enter matlab commands directly at the prompt or execute m-file programs.

4. Go back to the x-term window (where the athena prompt is and start the emacs text editor by typing emacs & and hitting enter. When the box outline appears hit enter again.

athena% emacs & (return)

Click in the emacs window;  you should be able to create and save text files.

Note: files must be saved with a .m extension for matlab to recognize and run them.

With both matlab and emacs running you are now ready to start answering the problems in your problem sets. A simple example is illustrated below.

Example m-file

First load the data files you will need by shift-clicking on the two following files demo1.dat and demo2.dat. You should be prompted for the location where these files will be saved; make sure they go to your modclass directory. Once you have the files in your modclass directory you can bring them into matlab by typing load demo1.dat (return) and load demo2.dat (return) at the matlab prompt.

>>load demo1.dat (return)
>>load demo2.dat (return)

Now type demo1 at the matlab prompt; what happens?

>>demo1 (return)

By loading the demo files into matlab, you have created a variable of the same name that is a single column matrix with the values from demo1.dat.

Now type demo1(1,1) at the matlab prompt; what happens?

>>demo(1,1) (return)

By using the index information after the variable name, you can select specific values from a matrix. Note the first value is the row number and the second value is the column number.

Now type plot(demo1,demo2) at the matlab prompt.

>>plot(demo1,demo2) (return)

Note: you should be getting the idea that you have to type return to get matlab to take the command so I will exclude it from here on in.

Now type the following and watch what happens.

>> title('Demoplot')
>> xlabel('demo1')
>> ylabel('demo2')
>> text(10,100,'here is some text')

From this last item, you see that you can put text information right on the plot, suppose you need to be more long-winded? All you need to do is make yor answer a string.

a=('The information in the plot clearly shows the blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah...')

After you type your well thought out answer and make it a string, to get it to display in matlab all you have to do is type a

>>a

Suppose we wanted to put all of the above commands in 1 executable file?

If we type the following in the emacs window:

% This m-file is part of the problem set demo created by T. Kenna on 8/16/98
% note that lines that begin with % are ignored as program lines by matlab
%
% Here comes the real program
%
load demo1.dat % loads the first demo file
load demo2.dat % loads the second demo file
demo1(1,1) % prints the first member of demo1
figure(1)
plot(demo1,demo2)
title('Demoplot')
xlabel('demo1')
ylabel('demo2')
text(10,100,'here is some text')
a=('The information in the plot clearly shows the blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah...')

After you create and save this file as classdemo (make sure you save it in modclass folder), what happens if you now run the file in matlab?

>>classdemo

Now matlab does everything from downloading the data to plotting to making comments and writing longer answers

Suppose you had more than one plot for question one, and you wanted to give me control of the pace of the program. For example, after starting classdemo, I would see the first plot and see your longer answer a. If you put a pause command as the next line, this will suspend the program and give me time to read and make sense of what you have said.

Continuing in emacs with classdemo.m add the following lines:

'hit any key to resume program'
pause % This will suspend the program until you are ready to move on
figure(2)
plot(sin(demo1),tan(demo2),'*g')
title('The second plot')
xlabel('Sine of demo1')
ylabel('Tangent of demo2')
b=('Plot number 2 shows the extent to which plot number 2 was yada yada yada')

After saving these changes, run classdemo.m again. Are you getting the picture?

Once you have finished your problem set m-file(s), click on the instructor or teacher in the problem set assignment and it will bring up the netscape mailer. All you have to do is put in a subject line describing what you are handing in and then simply attatch your m-file to it. When I receive it, all I have to do is run your m-file and it should unfold in an orderly fashion.