March 14

# MATLAB struct: Everything You Need to Know

Tandose Sambo

Welcome back to the MATLAB “Matrix” series. Like Neo, I’m sure you believe in math and science and want to further your abilities to navigate this often complex domain in order to reach the next phase of your science, mathematical and engineering needs.

## Introduction to MATLAB Struct

Let’s start by analyzing the wonderful image above. In a graphical format, we see that there is a hierarchy of data. The image above is the moot of the entire discussion. From a first glance…we see that it is referring to a patient database, with associated patient files, and within the patient files are the relevant fields of data, containing values pertaining to the individual patients. The data tree, is a field of silos containing information that can subsequently be filtered and assessed by data analysts. The theme of today’s discussion is the organization of data into silos via the tool MATLAB struct. From the graphical to the mathematical, MATLAB is the ideal tool that enables background data organization to be facilitated by any quantitative field.

Array Data Structure is that very organized section of mathematics, that arranges data into rows and columns. Elements consisting of either values or variables  are collated and stored. Mathematical formulae can then be applied to the data in order to determine how the system will behave.

Using a computer science example, if an array of integer variables exists in a pool of data, they may be further compacted into a series of words, stored at specific locations called memory addresses. The memory address is the foundation of the array. The “Matrix” is a two dimensional grid of data. Additionally,  more detailed concepts such as tuples and vectors will emerge as you delve into the field. The uses of arrays are numerous, and form the backbone of various computer programming functions.

In the logical programming functions entailed in computer programs, arrays are used to implement data structures in an ordered format. Additionally, in terms of applications of array structures, external storage devices utilize the function of arrays, in order to facilitate memory storage.

Applications of Arrays

On a larger scale on the computer memory scene, database construction and operation is reliant on arrays in order to facilitate the most compact storage of data in the available server space. One dimensional arrays, containing elements known as records are used to store data. In computer science, data structures are generated from arrays. A list of critical data structures that utilize arrays include:

Lists: Lists or sequences fall into the category of abstract data types. They contain ordered, repeatable values who’s count in the flow is noted.

Heaps: A data-structure in the form of a data tree. Parent nodes and their children are connected in an organized sequence.

Hash Tables: This is a data structure that outlines the link between variables in a key to value orientation. Hash tables fall into the abstract data type category.

Deques: These are abstract data types, which constitute double ended queues. Data in the queue is arranged in a sequence, and the sequence can either be grown or expanded from the head or the tail of the queue.

Queues: These are abstract data types with associated priority for elements in the ordered sequence.

Stack: An abstract data type, which has a collection of elements that can be manipulated via the operations of push and pop.

Strings: A sequence of characters, which can be changed with time. The nature of the character can vary.

Vlists: A persistent data structure that combines the elements of two components in an optimized hybrid. These two elements are the indexing of arrays, and linked lists. The advantages of both are fused to create a superior data structure that operates in a dual function.

With simplicity and ease, using arrays to store your data is not a complex process. Although other storage options exist, they are often the go-to for data organization.

## What is MATLAB Struct?

The MATLAB system enables a series of structure data elements to be organized into arrays. This objective is facilitated via the MATLAB struct functionality. Using a fashion similar to that outlined in the programming language C, the user can organize their data according to the outlined commands below. The data will be organized into different categories depending on the user needs.

## MATLAB Struct: Brief Tutorial

Using the MATLAB struct function, the user can generate the ordered array of their data into containers known as fields. The following extract, influenced by the MATLAB workbook, will allows us to delve into the world of data organization. From the computer science field, data becomes information when it is organized. Fields therefore, can be used to place the relevant data into the relevant categories that pertain to their purpose. The data in a field can be organized according to the following syntax:

structName.fieldName.

### Example Number One

Let’s organize some data! After the initial theory…always comes the practice. The following is some MATLAB code for data organization into a new structure. Applying the theory above, the following will be used to enter data into two fields.

s.a = 1;                    …(1)

s.b = {‘A’,‘B’,‘C’}     …(2)

Starting with a fresh command window, lines (1) and (2) of code were entered into the MATLAB GUI. The process immediate processes, and displays the following data:

From the data, one can note that there are two fields (a) and (b). The fields have data associated with them. Field (a) contains the number one, while (b) contains the three letters { A, B, C}. The domains for the two are outlined, and the system now recognizes how the data is to be organized as it is entered into the MATLAB system. Over time, additional data sets can be added to the existing data in the fields.

An alternative way to create structures in MATLAB is via the struct function. From the MATLAB workbook, the following are the syntaxes:

### MATLAB Struct Syntax Variations

s = struct . Data organized via this function is organized in a scalar structure with no associated fields.

s = struct(field,value). Structure array creation is facilitated via this code. The two criteria associated with the field are highlighted in the brackets. They are the field and the value. Values are variable and can be alpha-numeric, or matrices.

s = struct(field1,value1,…,fieldN,valueN). This function is for multiple field generation.

s = struct([]). This is an empty domain. No fields are present.

s = struct(obj). This function creates a new field containing the elements of the function (obj).

Once the data organization tools are identified, the varying input variables that make up the data collection domain can be manipulated. These different nomenclatures for the domains are: fields, character vector, values, scalars , arrays and objects. The different classifications will be encountered on your MATLAB journey. For the purposes of this tutorial, we will focus on the simpler ones.

### Example Number Two

This is the second example from the MATLAB workbook on the structure function. In this endeavor, we will create three fields – ‘x’, ‘y’ and ‘title’. Their associated descriptions are associated with the fields. The associated graph, generated from the field data will be plotted. Let us enter the code into the MATLAB Command Window:

data.x = linspace(0,2*pi);

data.y = sin(data.x);

data.title = ‘y = sin(x)’

Code for the graph:

plot(data.x,data.y)

title(data.title)

The associated graph is highlighted below:

A more complicated example involves the creation of a non-scalar structure that contains several fields with data of differing values in the fields. Entering the following code into MATLAB yields:

field1 = ‘f1’;  value1 = zeros(1,10);

field2 = ‘f2’;  value2 = {‘a’, ‘b’};

field3 = ‘f3’;  value3 = {pi, pi.^2};

field4 = ‘f4’;  value4 = {‘fourth’};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4)

The data is organized according to its associated values. Both numeric and cell arrays are accounted for by the system, and similar domains are fused. The expansion of the workspace shows how the data and the fields of the differing segments are organized:

## CONCLUSIONS

With our surface skim into the world of structures, data organization will soon become a breeze. Scientists, Mathematicians and any other quantitative fields thrive on the fact that data can be organized into silos that can be manipulated. With organized data, further statistical analysis can be pursued. There are numerous examples where data organization will facilitate appropriate conclusions by the analysts. Insurance companies are the perfect organizations that use silos of data to categorize the general population into different age categories.

The data bases for insurance companies utilize the fact that different age groups are entered as customers are seeking information. Based on incomes and life expectancies, an insurance package issued to a single twenty five year old, will differ from that of a forty year old with a family and a mortgage. The applications are numerous in the fields that exist.

The first step is to organize the data into the relevant, fields and include the critical values that classify to be in that domain.  Mathematical formula can then be applied to the data in order to determine how the system will behave.

Reference Articles:

1. Wikibooks – MATLAB Programming: https://en.wikibooks.org/wiki/MATLAB_Programming/Arrays/Struct_Arrays
2. Expanded Tutorial on Mathworks: https://www.mathworks.com/help/matlab/ref/struct.html

Tandose Sambo is a Chemical Process Engineer, with a focus on improving process efficiency via operational improvements. Six-Sigma certified, and with a Design-focus and Data Analytics interest, she is a driven growing entrepreneur, with the intention to optimize industrial and business process operations. Her interests include sharing time with family and travelling.

Tandose Sambo