Lecture4_Computing
Lecture4_Computing
GET211
Emmanuel Ali
Ayibaemi Ledum Jaafaru Sanusi
November 3, 2024
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 1 / 28
Outline
1 Introduction
2 Introduction to MATLAB
3 Data Type
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 2 / 28
Components of a Program
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 3 / 28
MATLAB Program Components
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 4 / 28
MATLAB
Program
Components
Error Version
Scripts Functions Classes Numeric Character Structures Loops Conditionals Handling File I/O User I/O Graphics OOP Toolboxes MEX DocumentationTesting
Control
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 5 / 28
Program Hierarchy and Data Flow
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 6 / 28
Program Hierarchy and Data Flow
Main Program
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 7 / 28
Data Flow and Component Interaction
We describe the data flow among MATLAB components and highlight the
interactions between key system components.
Data Flow Section:
- The Input Components receive data, which is passed to Processing
Components for analysis or transformation.
- Processed results are sent to Output Components for display or further
action, and saved to Data Storage Components if needed.
- Stored data can be reloaded and used in subsequent operations, showing the
cyclical nature of data flow in a MATLAB program.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 8 / 28
Data Flow and Component Interaction
Data Results
Input Processing Output
Components Components Components
Save
Load
Control Store
ControlFlow StorageSystem
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 9 / 28
Program Development Lifecycle
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 10 / 28
Program Development Lifecycle
Program
Development
Development Execution
Phase Phase
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 11 / 28
Command Window Input
String Input To prompt for a string, you can specify the second argument as
’s’.
1 name = input ( ’ Enter ␣ your ␣ name : ␣ ’ , ’s ’) ; % User
enters a string
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 12 / 28
Command Window Output
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 13 / 28
Command Window Output
Using fprintf
The fprintf function provides formatted output, allowing you to control the
appearance of the output text.
1 name = ’ Alice ’;
2 age = 30;
3 fprintf ( ’ Name : ␣ %s , ␣ Age : ␣ % d \ n ’ , name , age ) ; %
Formatted output
Using sprintf
The sprintf function formats data into a string without displaying it
immediately. This can be useful for constructing messages that will be
displayed later.
1 outputString = sprintf ( ’ The ␣ result ␣ is : ␣ %.2 f ’ ,
result ) ; % Format as string
2 disp ( outputString ) ;
% Display the formatted string
Escape Characters: Special characters such as \n (newline) and \t (tab) can
be used to format text output.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 14 / 28
Exporting Data
MATLAB allows you to export data to various file formats, making it
easier to share results or save them for later use.
Writing to Text Files
You can write data to text files using the writetable or writematrix
functions.
1 data = [1 , 2 , 3; 4 , 5 , 6];
2 writematrix ( data , ’ outputData . csv ’) ; % Write matrix
to CSV file
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 15 / 28
Importing Data
MATLAB can read data from external files, which is useful for processing
datasets or configurations stored outside the workspace.
Reading Text Files
You can use various functions to read data from text files, such as readtable,
readmatrix, and readcell.
Using readtable This function reads data from a text file into a table.
1 data = readtable ( ’ data . csv ’) ; % Read data from a CSV
file
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 16 / 28
Text Files
‘fopen‘ opens a file, ‘fgets‘ reads a line at a time, and ‘fscanf‘ reads
formatted data.
Use fopen, fgets, fscanf for reading.
1 fid = fopen ( ’ output . txt ’ , ’r ’) ;
2 line = fgets ( fid ) ;
3 disp ( line ) ;
4 fclose ( fid ) ;
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 17 / 28
Binary File I/O
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 18 / 28
CSV Files
CSV (Comma-Separated Values) files are a common format for storing
tabular data.
The csvwrite() function writes numeric data to a CSV file.
1 % Create a 3 x3 magic square matrix
2 M = magic (3) ;
3
4 % Write the matrix to a CSV file
5 csvwrite ( ’ magic . csv ’ , M ) ;
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 19 / 28
Image File
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 20 / 28
MAT-File I/O
MATLAB’s native file format (‘.mat‘) stores variables in binary format
and is efficient for saving and loading MATLAB variables.
Saving Workspace Variables
You can save all workspace variables to a MAT-file for later use using
the save function.
1 save ( ’ myWorkspace . mat ’) ; % Save all variables to a
. mat file
- Loading Data:
1 load ( ’ myData . mat ’) ;
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 21 / 28
Exercise
Write a MATLAB script that prompts the user to enter their name
and age.
- Use the ‘input‘ function to get this information.
- Display a greeting message that includes the user’s name and age
using the ‘disp‘ function.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 22 / 28
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 23 / 28
Exercise
Write a script that prompts the user for the radius of a circle and
calculates its area.
- Use the formula: Area = π × r2 .
- Output the area in a formatted string.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 24 / 28
Exercise
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 25 / 28
Exercise
Create a script that prompts the user to enter five test scores.
- Store these scores in a numeric array and write the array to a CSV
file named ‘tests cores.csv‘.
- Display a message indicating the file has been created.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 26 / 28
Exercise
Write a simple quiz script that asks the user a question (e.g., ”What is
the capital of France?”) and provides multiple-choice answers.
Capture the user’s response using the ‘input‘ function and display it.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 27 / 28
Exercise
Create a script that asks the user for their scores in three subjects.
Calculate the average score and display the score.
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester November 3, 2024 28 / 28