User:Eml4500.f08.wiki1.brannon

Section 13 Text Strings, Error Messages, Input

Text strings can be entered into MATLAB using single quotation marks as follows:

string='text string'

Using the disp function, text strings can be displayed in the following way:

disp('displays text string')

Error messages can be displayed with the error function:

error('error message')

which is useful with M-files, as an M-file execution will be aborted.

Within M-files, the user may be prompted to input data using the input function:

variable=input('Enter number: ')

The variable is assigned the input value and execution continues.

Section 14 Managing M-files

Creating or editing an M-file from within MATLAB can be accomplished using the !-feature. This is useful because all variables would be reset upon exiting MATLAB. System sommands such as copying, editing, and printing can be executed using this feature. As an example, !ed can be used to access the system editor:

>> !ed fileforediting.m

After editing the file, the user will return to MATLAB as it was before entering the editor.

In addition, the editor and MATLAB may be active simultaneously on systems permitting multiple processes, suspending one while working in another.

Debugging tools can be seen using the help dbtype command.

The present working directory can be viewed with the pwd command and cd can be used to change directories. The dir and ls commands list the contents of the directory and what lists the M-files within the directory only. The delete command deletes a diskfile. The type command prints a M-file to the screen.

M-files must be stored in a MATLAB-accesible directory, such as the present working directory. Typically, these files are stored in a subdirectory of the home directory titled matlab.

Section 15 Comparing Efficiency of Algorithms: Flops, Tic and Toc

Efficiency can be measured by counting the floating point operations (flops)performed or the elapsed time.

The flops count is reset using flops(0). Using the command flops again immediately after the operation gives the flops count.

flops(0), var=2+2; flops

The time elapsed for any given command can be measured using the tic command immediately before the command and toc just after it.

tic, command, toc

The processing speed available for the operation will also affect these measurements.

Section 16 Output Format

Computations within MATLAB are performed using double precision, though the format with which the results are shown can be changed with the following commands:

format short          fixed point with four decimal places (default) format long           fixed point with fourteen decimal places format short e        scientific notation with four decimal places format long e         scientific notation with fifteen decimal places format rat            approximation by ratio of small integers format hex            hexadecimal format format bank           fixed dollars and cents format +              +, -, blank

Changes to format remain until changed again.

Section 17 Hardcopy

Hardcopy is obtained using the diary command. The command below causes text that appears subsequently on the screen to be written to the named diskfile (default: diary):

diary named diskfile

The command diary on will write text to the file until the command diary off is called.