MATLAB MODULE 1

MATLAB Window Environment and the Base Program

 

Script files (M-files)

Type edit on MATLAB prompt and hit enter (or follow File New M-File option from the main menu bar or click on   icon in main toolbar). An Editor/Debugger window will open. This is where you write, edit, create, can run from, and save your own programs (user created script files with sequences of MATLAB commands) in files called M - files . An example M-file is shown in Fig. M1.19.

Create the same file in your MATLAB editor and then use the option File Save or File Save As to save the file with the name decayed_sin.m in current working directory. You can save all files into your personalized directory. If your personal directory is immediately below the directory in which the MATLAB application program is installed ( e.g. , c:\MATLAB7p0), then all user written files are automatically accessible to MATLAB. If you want to store files somewhere else, then you need to specify the path to the files using the path or addpath command, or change the current working directory to the desired directory before you run the program. For example, your script file is in the directory my_dir, which is not the current working directory of the MATLAB. If the location of my_dir is ‘ c:\docume~1\control\ my_dir ', it can be included in the MATLAB search path by:

>> path(path, ‘c:\docume~1\control\my_dir');

or

>> addpath ‘c:\docume~1\control\my_dir';

To remove specified directory from the MATLAB search path, use the command rmpath . Learn more about MATLAB search path through online help.

Type simply the name of the file decayed_sin to execute it from the command window. Script can also be saved and executed simultaneously by clicking the   icon in the main toolbar.

To open the existing M-file from the MATLAB command window, type edit filename (or follow File Open option from the main menu bar or click on   icon in   the main toolbar).

All variables created during the runtime of the script file are left in the workspace. Using who or whos , you can get information about them, and also access them by workspace window.

 

Exercise M1.10

Modify the script file written in Fig. M1.20 in the following way:

  1. An error message should prompt, if final time is less than initial time

  2. In addition to initial and final time, and time constant, the program should prompt the user to enter the amplitude of the sine wave.

  3. It should plot the Yt curve; however, wherever Yt crosses the zero-axes, an indication through x -mark should appear.

  4. Display current date and time at the end. (Hint: Learn commands date and clock using online help).

  5. Calculate the total execution time of the program. (Hint: Learn tic and toc commands).

 

Exercise M1.11

Learn more about 1) For loops, 2) While Loops, and 3) If-Else-End constructions using online help.

 

 

 

Fig. M1.19 Example M-file