AF

Lecture Notes 3

Chapter 3: Introduction to MATLAB Programming

3.1 Overview of MATLAB Scripts

  • When multiple commands are needed for a final result, it is better to group them in a MATLAB script.

    • Computer Program: A sequence of instructions that accomplishes a task.

    • Execution: The computer follows the instructions sequentially.

3.2 MATLAB Scripts

  • A script is a sequence of MATLAB instructions stored in an .m file.

    • Execution of a Script: Simply enter the name of the file (without the .m).

    • File Naming Rules: Same as variable naming; must start with a letter, followed by letters, digits, or underscores.

3.3 Creating a Simple Script

  • Example: Create a script script1.m to calculate the area of a circle.

    radius = 5;
    area = pi * radius^2;
  • Viewing the Script:

    • Open in the Editor Window or use the type command in the Command Window.

3.4 Documenting Scripts

  • Importance of Documentation: All scripts should be well-documented.

    • Use % at the beginning of a line for comments.

    • Example of documentation:

    % This script calculates the area of a circle.
    % First the radius is assigned
    radius = 5;
    area = pi * (radius^2);
    • Help Command: Displays the first block of comments from the script.

3.5 Input and Output in MATLAB

  • Input Function:

    • The simplest function is input(), which takes user input.

    rad = input('Enter the radius: ');
    • For string input, use 's' as a second argument.

3.6 Example Input Commands

  • Enter a vector or matrix:

    v = input('Enter a vector:');
    A = input('Enter a matrix:');

3.7 Output Functions

  • Output Functions: disp and fprintf.

    • disp: Basic output without formatting.

    disp('Hello');
    • fprintf: Supports formatted output.

    fprintf('The value is %d, for sure!

', 4^3);


### 3.8 Placeholders in Formatted Output
- Common placeholders:
- `%d`: integer 
- `%f`: float 
- `%c`: single character 
- `%s`: string
- **Example of Formatted Output:**
```matlab
fprintf('The int is %d and the char is %c
', 30, 'x');

3.9 Scripting with Input and Output

  • Example script sphereIO.m calculates volume:

    radius = input('Please enter the radius: ');
    volume = (4/3) * pi * radius^3;
    fprintf('The volume is %.2f inches cube

', volume);


### 3.10 Plotting with MATLAB
- **Basic Plot:** Using `plot()` function.
- For multiple points, create vectors:
```matlab
x = 1:6;
y = [1 5 3 9 11 8];
plot(x,y);

3.11 Customizing Plots

  • Customizing colors, line types, and markers.

    • Colors: r (red), g (green), b (blue), etc.

    • Line types: - (solid), -- (dashed), : (dotted).

3.12 Working with Figure Windows

  • Commands to manage figure windows:

    • clf: Clear current figure.

    • figure(n): Create new figure or specify figure number.

    • hold on/off: To retain plots.

3.13 File Input/Output

  • Modes of file operations: reading, writing, appending.

  • Writing Data: save command with -ascii qualifier.

  • Reading Data: Using load command for matrices.

3.14 Example of File I/O

  • Example of writing and loading data from a file.

    save testfile.dat mymat -ascii;
    mymat = load('testfile.dat');

3.15 User-Defined Functions

  • Function definitions include:

    • function keyword, output argument, function name, input arguments, and body.

  • Example Function calcarea:

    function area = calcarea(rad)
    area = pi * rad^2;
    end;