Introduction to MATLAB and the Environment

MATLAB is an interpreted, dynamically typed language built around matrices.

Concept

What MATLAB Is MATLAB is an interpreted, dynamically typed language built around matrices. You type a command and see the result immediately, or save commands in scripts (.m files) and functions. Its strengths:

Concise mathematics: x = A \ b solves a linear system.

A huge library of built-in, well-tested numerical functions.

Integrated plotting and visualization.

Toolboxes for signal processing, control, optimization, statistics, image processing, deep learning, automotive (Vehicle Network, Powertrain Blockset), and Simulink for block-diagram simulation and code generation.

It is not a general-purpose systems language: it isn't used for operating systems, web back ends or small microcontroller firmware directly (though it can *generate* C code for them).

Command Window Type commands, see results (>> prompt) Write scripts and functions, set breakpoints

Variables currently in memory, with size and class Current Folder

The folder MATLAB searches first for your files Command History Help / Documentation help name, doc name Scripts vs Live Scripts vs Functions

Script (.m): a sequence of commands sharing the base workspace.

Live Script (.mlx, MATLAB only): code, formatted text, equations and plots in one document — good for reports.

Function (.m with a function line): its own workspace, inputs and outputs (Module 11).

clc clears the Command Window, clear removes variables, close all closes figures, who/whos list variables, help/doc show documentation, format long/format short change display precision, Ctrl+C stops a running command.

Output: Semicolons, disp and fprintf A statement without a semicolon displays its result; with a semicolon it runs silently. For controlled output use disp(value) or fprintf(format, values...), which works like C's printf: %d integers, %f/%.2f fixed-point, %g compact, %e exponential, %s text, \n newline.

Why It Matters

Knowing the environment and the output functions is the foundation for everything else — and fprintf formatting is how you produce clean reports, logs and test results.

Syntax

x = 5;                         % assignment, no output
x                              % display the value
disp('text')                   % display text or a value
fprintf('%s = %.2f\n', 'x', x) % formatted output
help sqrt                      % documentation in the Command Window

Example

1.1 The first script

% hello.m - your first MATLAB script
disp('Hello, SampleApp!')

speed = 120;                       % a variable (semicolon: don't echo it)
fprintf('Vehicle speed: %d km/h\n', speed)
fprintf('That is %.2f m/s\n', speed / 3.6)

version_info = ver('Octave');      % in MATLAB use ver('MATLAB') or version
fprintf('Interpreter used for this course: %s %s\n', version_info.Name, version_info.Version)

Expected Result

The last line shows the interpreter that produced this course's outputs. In MATLAB, ver('MATLAB') or version reports the MATLAB release instead.

Hello, SampleApp!
Vehicle speed: 120 km/h
That is 33.33 m/s
Interpreter used for this course: Octave 11.3.0

Common Mistakes

Forgetting the semicolon in a loop and flooding the Command Window.

Naming a script like a built-in function (sum.m, plot.m), which shadows the built-in.

Running a script from the wrong current folder ("Undefined function or variable").

Using disp for formatted numbers; use fprintf or sprintf for control over format.

Best Practices

Start scripts that you share with a comment describing their purpose, inputs and outputs.

Keep your project files in one folder and make it the current folder (or add it with addpath).

End output lines with \n in fprintf.

Interview Relevance

Low to medium: "what is MATLAB used for?", "script vs function?", "what does the semicolon do?", "disp vs fprintf?".

Practice Questions

Print your name, age and height with one fprintf call.

Compute and print the area of a circle of radius 2.5 with 3 decimal places.

Find out what format long does to pi.

Use help to find what linspace does.

Quick Quiz

Q1. (Multiple choice) What does a semicolon at the end of a statement do? A) Ends the program B) Suppresses the display of the result C) Comments the line D) Nothing Answer: B.

Q2. (Predict the output) fprintf('%.2f\n', 120 / 3.6) Answer: 33.33 (verified in the example).

Q3. (True/False) MATLAB variable names are case-sensitive.

Answer: True. Q4. (Find the bug) A user saves their script as plot.m and now every call to plot runs their script.

Answer: The script shadows the built-in plot; rename the file.

Coding Challenge

Write a script that converts a list of speeds in km/h ([30 50 80 120]) to m/s and prints one line per value in the form 30 km/h = 8.33 m/s.

kmh = [30 50 80 120];
for k = 1:numel(kmh)
    fprintf('%d km/h = %.2f m/s\n', kmh(k), kmh(k) / 3.6)
end