MATLAB - Variables
MATLAB - Variables and Data Types
Section titled “MATLAB - Variables and Data Types”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.
Creating and Assigning Variables
Section titled “Creating and Assigning Variables”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:
4If 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 calculationresult = ans * 5Best Practices for Variable Naming
Section titled “Best Practices for Variable Naming”- Be Descriptive: Use names that describe the variable’s purpose, like
initialVelocityorcustomerNameinstead ofvorc. - 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
myVaris different frommyvar. - 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 usingexist('name').
Managing Workspace Variables
Section titled “Managing Workspace Variables”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 varNameremoves a specific variable, whileclearon its own removes all variables.
% Create a few variablesstudent_name = "Alex";grades = [95, 88, 92];
% See what's in the workspacewhosExpected Output:
Name Size Bytes Class Attributes
grades 1x3 24 double student_name 1x1 138 stringCommon Data Types
Section titled “Common Data Types”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: Representstrue(1) orfalse(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 Vectors and Matrices
Section titled “Creating Vectors and Matrices”Creating arrays is a core MATLAB skill. Here are the modern and most common methods.
Manual Creation
Section titled “Manual Creation”% 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];Using Functions
Section titled “Using Functions”% Create a vector of 10 evenly spaced points from 0 to 1lin_vec = linspace(0, 1, 10);
% The colon operator: create a vector from 1 to 10 with a step of 2colon_vec = 1:2:10; % Result: [1 3 5 7 9]
% Create a 3x4 matrix of all zeroszero_mat = zeros(3, 4);
% Create a 2x2 identity matrixident_mat = eye(2);Controlling Output Format
Section titled “Controlling Output Format”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 placesdisp(my_val) % Displays 33.3333
format long % 15 decimal places for doubledisp(my_val) % Displays 33.333333333333336
format bank % 2 decimal placesdisp(my_val) % Displays 33.33
format rat % Closest rational fractiondisp(my_val) % Displays 100/3