Skip to content

MATLAB - Variables

In MATLAB, a variable is a named storage location in memory. The fundamental principle is that every variable in MATLAB is an array, which can hold numbers, text, or more complex data. Even a single number is treated as a 1x1 array.

Assignment is done using the equals sign (=). MATLAB automatically determines the appropriate data type for the variable based on the value you assign.

% Assign a single numeric value (a 1x1 double array)
x = 16;
% The semicolon at the end suppresses output to the Command Window
% This is best practice for writing clean scripts.
y = sqrt(x);
% To see the value of y, simply type its name or use disp()
disp(y)

Output:

4

If you execute an expression without assigning it to a variable, MATLAB stores the result in a temporary variable called ans.

% This calculation's result is stored in 'ans'
log(100)
% You can use 'ans' in the next calculation
result = ans * 5
  • Be Descriptive: Use names that describe the variable’s purpose, like initialVelocity or customerName instead of v or c.
  • Start with a Letter: Variable names must start with a letter, followed by any combination of letters, numbers, and underscores.
  • Case-Sensitive: MATLAB is case-sensitive, so myVar is different from myvar.
  • Avoid Keywords: Do not use names of built-in functions or keywords (e.g., if, for, plot). You can check if a name is a function using exist('name').

As you create variables, they populate the MATLAB workspace. You can manage them with these commands:

  • who: Lists the names of all variables currently in the workspace.
  • whos: Provides a detailed list, including variable name, size (dimensions), memory usage, and data type.
  • clear: Removes variables from memory. clear varName removes a specific variable, while clear on its own removes all variables.
% Create a few variables
student_name = "Alex";
grades = [95, 88, 92];
% See what's in the workspace
whos

Expected Output:

Name Size Bytes Class Attributes
grades 1x3 24 double
student_name 1x1 138 string

While double (64-bit floating-point number) is the default, MATLAB supports many other data types:

  • double: Default for numbers, e.g., x = 3.14.
  • string: For text, enclosed in double quotes. Ideal for manipulating text data. E.g., s = "Hello, MATLAB!".
  • char: For single characters or character arrays, in single quotes. E.g., c = 'H'.
  • logical: Represents true (1) or false (0). E.g., isValid = true.
  • cell: An array that can hold different data types in each element. E.g., C = {1, "text", [2 3]}.
  • struct: A structure with named fields for organizing related data. E.g., patient.name = "John"; patient.age = 45;.

Creating arrays is a core MATLAB skill. Here are the modern and most common methods.

% A row vector (elements separated by commas or spaces)
row_vec = [1, 2, 3, 4, 5];
% A column vector (elements separated by semicolons)
col_vec = [1; 2; 3; 4; 5];
% A 2x3 matrix (semicolon starts a new row)
my_matrix = [1, 2, 3; 4, 5, 6];
% Create a vector of 10 evenly spaced points from 0 to 1
lin_vec = linspace(0, 1, 10);
% The colon operator: create a vector from 1 to 10 with a step of 2
colon_vec = 1:2:10; % Result: [1 3 5 7 9]
% Create a 3x4 matrix of all zeros
zero_mat = zeros(3, 4);
% Create a 2x2 identity matrix
ident_mat = eye(2);

The format command controls how numbers are displayed in the Command Window. This does not change the stored value, only its appearance.

my_val = 100/3;
format short % Default, 4 decimal places
disp(my_val) % Displays 33.3333
format long % 15 decimal places for double
disp(my_val) % Displays 33.333333333333336
format bank % 2 decimal places
disp(my_val) % Displays 33.33
format rat % Closest rational fraction
disp(my_val) % Displays 100/3