← MATLAB EnglishChapter 02 of 13

Variables and Data Types

## Learning Objectives - Understand MATLAB data types - Create and manipulate variables - Work with arrays and cell arrays - Use structures for complex data ## Data Types Overview ### Basic Data Types | Type | Example | Description | |------|---------|-------------| | double | `3.14` | Double-precision floating point | | single | `3.14` | Single-precision floating point | | int8 | `42` | 8-bit signed integer | | int16 | `42` | 16-bit signed integer | | int32 | `42` | 32-bit signed integer | | int64 | `42` | 64-bit signed integer | | uint8 | `42` | 8-bit unsigned integer | | char | `'A'` | Character | | logical | `true` | Boolean (true/false) | | string | `"hello"` | String (R2016b+) | ## Numeric Types ### Floating Point ```matlab % Double precision (default) x = 3.14159; class(x) % Returns 'double' % Single precision y = single(3.14159); class(y) % Returns 'single' % Precision comparison eps('double') % ~2.22e-16 eps('single') % ~1.19e-7 ``` ### Integer Types ```matlab % Signed integers a = int8(127); % Range: -128 to 127 b = int16(32767); % Range: -32768 to 32767 c = int32(2^31-1); d = int64(2^63-1); % Unsigned integers e = uint8(255); % Range: 0 to 255 f = uint16(65535); g = uint32(2^32-1); h = uint64(2^64-1); ``` ### Checking Types ```matlab x = 42; whos x % Shows size, bytes, class isa(x, 'double') % Returns true isa(x, 'integer') % Returns false ``` ## Variables ### Variable Assignment ```matlab x = 10; % Simple assignment y = x * 2; % Assignment with computation z = (1:5); % Assignment with array ``` ### Variable Info ```matlab a = 5; b = 'hello'; whos % List all variables clear a % Remove specific variable clear all % Remove all variables ``` ## Arrays ### Creating Arrays ```matlab % Row vector row = [1, 2, 3, 4, 5] % Column vector col = [1; 2; 3; 4; 5] % 2D array A = [1, 2, 3; 4, 5, 6; 7, 8, 9] % Using colon operator v = 1:2:10 % 1, 3, 5, 7, 9 (start:step:end) w = 0:0.1:1 % 0, 0.1, 0.2, ... 1.0 ``` ### Array Indexing ```matlab A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A(1, 1) % First element: 1 A(3, 3) % Last element: 9 A(1, :) % First row: [1, 2, 3] A(:, 2) % Second column: [2; 5; 8] A(1:2, 1:2) % Top-left 2x2 submatrix ``` ### Array Size ```matlab A = [1, 2, 3; 4, 5, 6]; size(A) % [2, 3] - rows, columns length(A) % 3 - largest dimension numel(A) % 6 - total number of elements ndims(A) % 2 - number of dimensions ``` ## Cell Arrays Cell arrays can hold different types of data in each cell. ### Creating Cell Arrays ```matlab % Using curly braces c = {1, 'hello', [1, 2, 3]} % Using cell function c = cell(1, 3); c{1} = 42; c{2} = 'MATLAB'; c{3} = magic(3); ``` ### Accessing Cell Arrays ```matlab c = {'apple', 'banana', 'cherry'}; c{1} % Returns 'apple' (content) c(1) % Returns {'apple'} (cell) ``` ### Converting Between Arrays and Cells ```matlab % Array to cell arr = [1, 2, 3, 4, 5]; c = num2cell(arr) % Cell to array c = {1, 2, 3, 4, 5}; arr = [c{:}] ``` ## Structures Structures organize data using named fields. ### Creating Structures ```matlab % Direct assignment person.name = 'John'; person.age = 30; person.email = 'john@example.com'; % Using struct function person = struct('name', 'John', 'age', 30, 'email', 'john@example.com'); ``` ### Array of Structures ```matlab % Create multiple structures people(1).name = 'Alice'; people(1).age = 25; people(2).name = 'Bob'; people(2).age = 30; % Access all names names = {people.name} % {'Alice', 'Bob'} ``` ### Nested Structures ```matlab employee.name = 'John'; employee.dept.name = 'Engineering'; employee.dept.location = 'Building A'; ``` ## Logical Type ```matlab % Creating logical values isValid = true; isEmpty = false; % Logical arrays logicVec = [true, false, true, true]; % Converting to logical x = [0, 1, 2, -3]; y = logical(x) % [false, true, true, true] % Finding true values find(logicVec) % Returns [1, 3, 4] ``` ## Type Conversion ```matlab x = 42; % To string s = string(x) s = num2str(x) s = int2str(x) % To number str = '123'; n = str2double(str) n = str2num(str) % To integer (with rounding) y = 3.7; int8(y) % 4 (rounds) floor(y) % 3 (floor) ceil(y) % 4 (ceiling) round(y) % 4 (round) ``` ## Empty Arrays ```matlab emptyVec = [] % 0x0 empty array emptyMat = zeros(0, 5) % 0x5 empty matrix isempty(emptyVec) % true numel(emptyVec) % 0 ``` ## Summary - MATLAB has multiple numeric types: double, single, and integers - Use `class()` to check type, `isa()` to test type - Arrays use parentheses for indexing `A(row, col)` - Cell arrays use curly braces `c{index}` for contents - Structures use dot notation `struct.field` - Type conversion functions: `string()`, `num2str()`, `str2double()` - Empty arrays created with `[]` or `zeros(0,n)`

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →