Input/Output (I/O)

 

Diary and workspace files

 

·       MATLAB allows you to record the command window input and output activity during a work session. These files can be imported into most word processors such as Word. To begin this process you would type,

diary session1.txt

 

·       To terminate the recording process you would type

diary off

 

·       To start the diary recording where you left off before a diary off command use

diary on

 

·       MATLAB also lets you save and reload your workspace variables. To save your workspace into a file called output1.mat (for example), you would type

save output1

 

·       To reload the workspace variables from output1.mat at a later stage you simply type,

load output1

 

Displaying text and numerical values in the command window

 

·       By simply typing a variable name with no semicolon at the end, this variable is displayed in the command window.  However, you may want to add text to this or format the numerical output in a specific way (2 decimal places, no decimal, scientific notation, etc.).

 

·       To display sensible “english language” type statements in the command window you may want to use the command disp( ).  It will also print numerical variables.  Here is how it is used:

disp('Here is the solution to our problem: ')

disp(sqrt(10))

 

·       The format command changes the format of data that is printed in the command window. Table 3.3-2 (pg. 94) details the different types of formats available. The short format (default) is commonly used although long, short e (scientific notation) and bank (good for money, 2 decimal places) are also commonly used.  Here is an example:

format bank

x=1000.1234

 

format long

pi

·       Typing format, with nothing following, puts you back into short format.

 

·       The fprintf( ) command allows you to print string text and numerical data in a precisely formatted manner. The syntax and formatting options for this command are detailed in Table 3.3-3 (pg. 95).

 

          fprintf('The square of %.1f is %.1f \n',3.2345,3.2345^2)

 

          x=[12.5678,5.76532];

          fprintf('The first element of x is %.2f and the second is %.2f \n',x)

 

Note that %.1f  means that numerical values are to be printed as floating point decimals with one decimal place of precision.  The \n at the end is a “new-line” character, it moves you to a new line in the command window, ready for more input or printing.

 

·       The sprintf( ) command is very similar to fprintf( ).  However, sprintf( ) is used to store the resulting text string as a MATLAB variable for use later.  This is often used to put text with numbers on plot axes, titles, etc.  Try this…

 

S=sprintf('The first element of x is %.2f and the second is %.2f \n',x);

S

         

The input command

 

·       You can put commands in an m-file which prompt you (the user) for various inputs.  This may be nice if you want to try a variety of values for some operation and you don’t want to edit the m-file each time.  Let’s say we want the user to define a variable x, which is to be used by subsequent commands.    Here is the line you would include in you m-file:

 

x=input('Please enter the variable x: ');      

 

The pause command

 

·       Sometimes in your m-file, you want your program to pause so you can look at some result before moving on to the next.  Maybe you plot a figure and you want a chance to look at the figure before you program moves on and perhaps plots a new figure.  The ‘pause’ command will do this for you.  By putting the command pause in your m-file, the program will stop executing at that point until you hit some key on the keyboard.

 

The figure command

 

·       If you are plotting several functions and you want them on separate figures, you need to use the figure command.  The command figure, brings up a new figure window and makes it the current window.  Thus, any new plot will appear here.  Plots will keep appearing and overwriting on this figure window, until you execute the figure command again.

 

·       If you want to be specific about where you plot, use figure(1), figure(2), figure(3), etc.  The command figure(num) makes figure window number ‘num’ become the current window.  If figure number num’ does not yet exist, MATLAB will open one.

 

The print command

 

·       The print command lets you print or save the current figure in many formats.  In its simplest use, type print at the command line and it will print the current figure (the one on top) to the printer in the back of the lab.  This will be sufficient for our purposes.

 

·       To print output from the command window, highlight the stuff you want in the command window with the mouse.  Then go to File àPrint Selection.