
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Structures
In MATLAB, structure arrays are a powerful data structure that allows you to organize and manipulate collections of related data using a combination of fields and indices. Structure arrays provide an efficient way to manage and process multiple instances of similar data with varying attributes, making them essential for various tasks, from data storage to complex analyses.
A structure array is an array where each element is a structure. Each structure within the array can contain multiple fields, and these fields hold different types of data, including numbers, text, logical values, and even other structures. Structure arrays are particularly useful when you have a group of similar objects with distinct characteristics.
MATLAB struct() Function
In MATLAB, the struct() function provides a convenient and versatile way to create structure arrays.
Syntax
s = struct(field,value)
The struct() function enables you to create structure arrays by specifying field names and corresponding values in a single call.
Create Structure Array
Let us see a simple example of creating a structure array using struct() function.
Example
Let us create a structured array that stores information about student names and ages.
names = {'Riya', 'Tiya', 'Siya'} ages = [20, 22, 19] students = struct('Name', names, 'Age', ages)
In this example, the student structure array has two fields, 'Name' and 'Age', with corresponding values assigned using cell arrays and numeric arrays.
When you execute the same in matlab command window the output is −
>> names = {'Riya', 'Tiya', 'Siya'} ages = [20, 22, 19] students = struct('Name', names, 'Age', ages) names = { [1,1] = Riya [1,2] = Tiya [1,3] = Siya } ages = 20 22 19 students = 1x3 struct array with fields: Name Age
Multiple Fields in a Structure Array
Here is an example to add multiple field to a structure array.
Following is the data we will have as fields and values.
field1 = 'a1'; value1 = zeros(1,10); − This creates a field named 'a1' and assigns a numeric array of zeros with 10 elements to value1.
field2 = 'a2'; value2 = {'a', 'b'}; − This defines a field named 'a2' and assigns a cell array containing character strings 'a', 'b' to value2.
field3 = 'a3'; value3 = {pi, pi.^2}; − This declares a field named 'a3' and assigns a cell array with two elements: the mathematical constant pi and the square of pi (pi^2).
field4 = 'a4'; value4 = {'testing'}; − This establishes a field named 'a4' and assigns a cell array containing the string 'testing' to value4.
field1 = 'a1'; value1 = zeros(1,10); field2 = 'a2'; value2 = {'a', 'b'}; field3 = 'a3'; value3 = {pi, pi.^2}; field4 = 'a4'; value4 = {'testing'}; s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
When you execute the above code the resulting structure s, contains four fields: 'a1', 'a2', 'a3', and 'a4'.
>> field1 = 'a1'; value1 = zeros(1,10); field2 = 'a2'; value2 = {'a', 'b'}; field3 = 'a3'; value3 = {pi, pi.^2}; field4 = 'a4'; value4 = {'testing'}; s = struct(field1,value1,field2,value2,field3,value3,field4,value4) s = 1x2 struct array with fields: a1 a2 a3 a4
We have cell arrays for value2 and value 2 and the size is 1-by-2 hence he size of s is 1-by-2
So s(1) and s(2) the output is as follows −
s(1)
>> s(1) ans = struct with fields: a1: [0 0 0 0 0 0 0 0 0 0] a2: 'a' a3: 3.1416 a4: 'testing'
s(2)
>> s(2) ans = struct with fields: a1: [0 0 0 0 0 0 0 0 0 0] a2: 'b' a3: 9.8696 a4: 'testing'
Here is a list of few built-in functions available with structures that can help you in your code when working with structures.
Function | Description |
---|---|
fieldnames() | This method will give fieldnames used in the structure |
isfield() | This method checks whether the given input field belongs to the structure. |
isstruct() | This method will tell you if the given input is a structure array or not. |
rmfield() | This method removes the given field from the structure array. |
fieldnames()
This method will return the field names of the structure.
Syntax
fields = fieldnames(S)
Let us first create a structure and use the fieldnames() function on it.
names = {'Riya', 'Tiya', 'Siya'}; ages = [20, 22, 19]; students = struct('Name', names, 'Age', ages); fieldnames(students)
When you execute the code in matlab the output is −
>> names = {'Riya', 'Tiya', 'Siya'}; ages = [20, 22, 19]; students = struct('Name', names, 'Age', ages); fieldnames(students) ans = { [1,1] = Name [2,1] = Age }
isfield()
The method will return 1 if the given field name is the field in the structure and 0 if it is not.
Syntax
res = isfield(S,field)
Here S is the structure array and field is the field name that you are checking if it's the field in structure array S.
Example
names = {'Riya', 'Tiya', 'Siya'}; ages = [20, 22, 19]; students = struct('Name', names, 'Age', ages); isfield(students, 'Names')
When you test the code in matlab the output is −
>> names = {'Riya', 'Tiya', 'Siya'}; ages = [20, 22, 19]; students = struct('Name', names, 'Age', ages); isfield(students, 'Name') ans = 0
isstruct()
The function returns logical 1 (true) if the given input is a structure array and logical 0 (false) if not.
Syntax
a = isstruct(A)
Example
names = {'Riya', 'Tiya', 'Siya'}; ages = [20, 22, 19]; students = struct('Name', names, 'Age', ages); isstruct(students)
When you execute the same in matlab command window.
>> names = {'Riya', 'Tiya', 'Siya'}; ages = [20, 22, 19]; students = struct('Name', names, 'Age', ages); isstruct(students) ans = 1
Let us give some other input then the structure array to the function isstruct().
Example
A = [1,2,3;4,5,6;7,8,9] isstruct(A)
When you execute the above code in matlab command window the output is −
>> A = [1,2,3;4,5,6;7,8,9] isstruct(A) A = 1 2 3 4 5 6 7 8 9 ans = 0
rmfield()
The function will remove the given field from the structure array.
Syntax
s = rmfield(S,field)
Here S is a structure array and field is the fieldname you want to remove from the structure array.
Example
field1 = 'a1'; value1 = zeros(1,10); field2 = 'a2'; value2 = {'a', 'b'}; field3 = 'a3'; value3 = {pi, pi.^2}; field4 = 'a4'; value4 = {'testing'}; s = struct(field1,value1,field2,value2,field3,value3,field4,value4) t = rmfield(s, field4)
When you execute the above code in matlab the output is −
>> field1 = 'a1'; value1 = zeros(1,10); field2 = 'a2'; value2 = {'a', 'b'}; field3 = 'a3'; value3 = {pi, pi.^2}; field4 = 'a4'; value4 = {'testing'}; s = struct(field1,value1,field2,value2,field3,value3,field4,value4) t = rmfield(s, field4) s = 1x2 struct array with fields: a1 a2 a3 a4 t = 1x2 struct array with fields: a1 a2 a3