MATLAB - Commands
MATLAB - Modern Command-Line Usage
Section titled “MATLAB - Modern Command-Line Usage”MATLAB provides a powerful interactive environment for numerical computation, data analysis, and visualization. You can execute commands directly in the Command Window at the >> prompt. This tutorial covers essential commands for a modern MATLAB workflow.
We will explore commands for managing your session, interacting with the file system, handling I/O, and working with MATLAB’s core data structures.
Workspace and Session Management
Section titled “Workspace and Session Management”These commands help you manage the variables in your workspace and control your MATLAB session. The Workspace browser in the MATLAB Desktop provides a graphical interface for these operations.
| Command | Purpose |
|---|---|
clc | Clears the Command Window text, keeping the workspace intact. |
clear | Removes all variables from the current workspace. Use clear varName to remove a specific variable. |
exist | Checks for the existence of a variable, file, or folder. E.g., exist('myVar', 'var'). |
global | Declares variables to be global, accessible across different functions. Best Practice: Use with caution; passing variables as function arguments is generally preferred. |
help | Displays help text for a function in the Command Window. E.g., help plot. |
doc | Opens the full documentation page for a function, which is often more detailed than help. E.g., doc plot. |
lookfor | Searches the first line of help text for a keyword. Useful when you don’t know the exact function name. |
quit | Stops MATLAB. |
who | Lists the names of variables in the current workspace. |
whos | Lists current variables with details like size, bytes, and class. |
File System and Data I/O
Section titled “File System and Data I/O”Modern data science requires robust tools for importing and exporting data. MATLAB excels at handling various file formats, from simple text files to large datasets.
| Command | Purpose |
|---|---|
cd | Changes the current directory. Use cd .. to go up one level. |
dir | Lists the contents of the current directory. |
pwd | Displays the current working directory path. |
load / save | Loads and saves workspace variables to a MATLAB-specific .mat file. E.g., save('mySession.mat'). |
readtable | Modern Best Practice. Reads tabular data from files like .csv or .xlsx into a table data type. |
writetable | Modern Best Practice. Writes a table to a file like .csv or .xlsx. |
readmatrix / writematrix | Reads/writes purely numeric data from/to text or spreadsheet files. |
delete | Deletes a file. Use with caution. |
type | Displays the contents of a text-based file (e.g., a .m script). |
diary | Creates a log of commands and output from your MATLAB session. |
wklread | Deprecated. This function was used for older Lotus 1-2-3 spreadsheets. Use readtable or readmatrix for modern spreadsheet files. |
Command Window Input and Output
Section titled “Command Window Input and Output”These commands control how information is displayed in the Command Window and how user input is received.
| Command | Purpose |
|---|---|
disp | Displays the contents of an array or string without printing the variable name. Useful for showing messages. |
fprintf | Writes formatted data to the screen or a file, offering fine-grained control over the output. Similar to C’s printf. |
input | Displays a prompt and waits for the user to enter a value. |
format | Controls the numeric display format in the Command Window (e.g., format long, format short, format bank). |
; (semicolon) | Suppresses the output of a command. Essential for preventing large matrices from flooding the screen. |
Array, Matrix, and Table Commands
Section titled “Array, Matrix, and Table Commands”MATLAB’s core strength is its handling of arrays and matrices. The following commands are fundamental for data manipulation.
| Command | Purpose |
|---|---|
size | Returns the dimensions of an array. |
length | Returns the length of the largest array dimension. For a vector, it’s the number of elements. |
numel | Returns the total number of elements in an array. |
reshape | Changes the size and shape of an array while preserving the number of elements. |
zeros, ones, eye | Creates arrays of all zeros, all ones, or an identity matrix, respectively. |
linspace, logspace | Creates linearly or logarithmically spaced vectors. |
max, min, sum, prod | Computes the maximum, minimum, sum, or product of array elements. |
sort | Sorts array elements in ascending order. |
find | Finds indices and values of nonzero elements, or elements that satisfy a logical condition. |
det, inv, rank | Computes the determinant, inverse, and rank of a matrix. |
cross, dot | Computes vector cross products and dot products. |
table | Modern Data Structure. Creates a table, which can hold columns of different data types (numeric, text, categorical). |
cell | Creates a cell array, which can hold different types and sizes of data in each element. |
Modern Plotting Commands
Section titled “Modern Plotting Commands”MATLAB offers a powerful and flexible graphics system. The modern approach involves creating plot objects and modifying their properties for full control over visualization.
| Command | Purpose |
|---|---|
plot | Creates a 2D line plot. Returns a plot object for further customization. E.g., p = plot(x, y); p.LineWidth = 2;. |
scatter | Creates a 2D scatter plot with markers. |
bar | Creates a bar chart. |
histogram | Creates a histogram to visualize data distribution. |
heatmap | Creates a heatmap from a matrix. |
xlabel, ylabel, title, legend | Adds labels, a title, and a legend to the current axes. |
figure | Creates a new figure window. |
tiledlayout | Modern Best Practice. Creates and arranges a grid of plots within a figure. Preferred over subplot. |
subplot | Creates subplots in a grid. tiledlayout is recommended for newer code. |
hold on / hold off | Allows multiple plots to be drawn on the same axes without overwriting. |
axis | Sets or gets the axis limits. E.g., axis([xmin xmax ymin ymax]). |
grid on / grid off | Toggles the visibility of grid lines. |
exportgraphics | Modern Best Practice. Saves a figure or axes to a file (e.g., PNG, PDF, EPS). Preferred over print. |