English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
MATLAB is an interactive program for numerical computation and data visualization. You can enter commands by typing them into the MATLAB prompt '>>' in the command window.
In this section, we will provide a list of commonly used general MATLAB commands.
MATLAB provides various commands for managing sessions. The following table provides all such commands-
Command | Purpose |
---|---|
clc | Clear the command window. |
clear | Delete variables from memory. |
exist | Check for the existence of files or variables. |
global | Declare variables as global variables. |
help | Search for help topics. |
lookfor | Search for keywords in the help entries. |
quit | Stop MATLAB. |
who | List current variables. |
whos | List current variables (long display). |
MATLAB provides various useful commands to handle the system, such as saving the current workspace as a file and loading the file later.
It also provides various commands for other system-related activities, such as displaying the date, listing files in the directory, and displaying the current directory, etc.
The following table displays some commonly used system-related commands-
Command | Purpose |
---|---|
cd | Change the current directory. |
date | Display the current date. |
delete | Delete files. |
diary | Enable/Stop recording diary file. |
dir | List all files in the current directory. |
load | Load workspace variables from a file. |
path | Display the search path. |
pwd | Display the current directory. |
save | Save workspace variables to a file. |
type | Display the content of the file. |
what | List all MATLAB files in the current directory. |
wklread | Read .wk1Spreadsheet files. |
MATLAB provides the following commands related to input and output-
Command | Purpose |
---|---|
disp | Display the content of an array or string. |
fscanf | Read formatted data from a file. |
format | Control the display format of the screen. |
fprintf | Execute formatted writing to the screen or file. |
input | Display a prompt and wait for input. |
; | Prohibit screen printing. |
fscanfandfprintfThe behavior of the command is like the C scanf and printf functions. They support the following format codes-
Format code | Purpose |
---|---|
%s | Format as a string. |
%d | Format as an integer. |
%f | Format as a floating-point value. |
%e | Format as a floating-point value in scientific notation. |
%g | Format in the most compact format: %f or %e. |
\n | Insert a new line in the output string. |
\t | Insert a tab in the output string. |
The format function has the following form for displaying numbers-
Format function | Maximum display |
---|---|
format short | Four decimal places (default). |
format long | 16Number of decimal places. |
format short e | Five digits plus exponent. |
format long e | 16Number of digits plus exponent. |
format bank | Two decimal places. Fixed-point currency format |
format + | Positive number, negative number, or zero. |
format rat | Rational approximation. Decimal fraction representation |
format compact | Prohibit certain line breaks. |
format loose | Reset to a less compact display mode. |
The following table shows various commands used for processing arrays, matrices, and vectors-
Command | Purpose |
---|---|
cat | Concatenate arrays. |
find | Find the index of non-zero elements. |
length | Calculate the number of elements. |
linspace | Create a vector with regular spacing. |
logspace | Create a vector with logarithmic spacing. |
max | Return the largest element. |
min | Return the smallest element. |
prod | Product of each column. |
reshape | Change size. |
size | Calculate the size of an array. |
sort | Sort each column. |
sum | Sum each column. |
eye | Create a unit matrix. |
ones | Create an array. |
zeros | Create a zero array. |
cross | Calculate the cross product of a matrix. |
dot | Calculate the dot product of a matrix. |
det | Calculate the determinant of an array. |
inv | Calculate the inverse of a matrix. |
pinv | Calculate the pseudoinverse of a matrix. |
rank | Calculate the rank of a matrix. |
rref | Calculate the reduced row echelon form of a matrix. |
num | Create a cell array. |
celldisp | Display the cell array. |
cellplot | Display the graphical representation of a cell array. |
num2num | cell |
Convert a numeric array to a cell array. | deal |
Match input and output lists. | Identify cell array. |
MATLAB provides many commands for plotting graphics. The following table shows some commonly used plotting commands-
Command | Purpose |
---|---|
axis | Set axis limits. |
fplot | Intelligent plotting of functions. |
grid | Show grid lines. |
plot | Generate an xy plot. |
Print the graph or save it to a file. | |
title | Place text at the top of the graph. |
xlabel | Add text labels to the x-axis. |
ylabel | Add text labels to the y-axis. |
axes | Create an axis object. |
close | Close the current graph. |
close all | Close all plots. |
figure | Open a new graph window. |
gtext | Enable label placement with the mouse. |
hold | Freeze the current graph. |
legend | Place the legend with the mouse. |
refresh | Redraw the current graph window. |
set | Specify the properties of an object (e.g., axis). |
subplot | Create a plot in a subwindow. |
text | Place a string in the graph. |
bar | Create a bar plot. |
loglog | Create a logarithmic plot.-Logarithmic plot. |
polar | Create a polar plot. |
semilogx | Create a semi-log plot. (Logarithmic X-axis). |
semilogy | Create a semi-log plot. (Logarithmic Y-axis). |
stairs | Create a staircase plot. |
stem | Create a stem plot. |