Compose-2025: Cells and Structs in OML

This tutorial explains how to:
  • Create a cell.
  • Modify element in a cell.
  • Create a struct.
  • Modify a struct.
  • Convert a struct to cell.
  • Convert a cell to struct.

Creating a Cell

Cells are different from matrices in that they can hold more than just a number or character. They can hold a complex number, string, matrix, or even another cell.
  1. From the Editor window, enter the following line in a new OML script:
    a = {[1,2;3,4],6-2i;'hello', [0,0,0]; false,{'I''m another cell'}}
    This creates a cell where:
    • a (1,1) is the matrix [1,2;3,4]
    • a (1,2) is the complex number 6-2i
    • a (2,1) is the string 'hello'
    • a (2,2) is the vector [0,0,0]
    • a(3,1) is false, which is a kind of Boolean
    • a(3,2) is another cell which only contains one string element ‘I’m another cell’
  2. Click Run to view the output:

Modifying an Element in a Cell

If you want to modify an element in an already-created cell, you can reference the element and reassign just that particular one.
  1. To reference a particular element in a cell, use a{x,y} with a being the name of the cell and x and y being the row and column, respectively. If you want to modify the element in a{2,1} add the following:
    a{2,1} = [5,6;7,8]
  2. Click Run to see the following results:

    In this case, we see that the element at [2,1] was changed from the string 'hello' to the matrix [5,6;7,8].

Create a Struct

Structures are a data type similar to cells. However, where cells use numeric indices to refer to specific pieces of data, structs use strings. These strings refer to entities called ‘fields’. Each field has its own value which can be of any datatype. Fields must be valid identifiers.
  1. From the Editor window, enter the following into a new OML script:
    student.name = ‘Bob’;      
    student.age = 16;      
    student.id = 8051

    or, you can use struct() to perform the same assignment:

    student = struct(‘name’,’Bob’,’age’,16,’id’, 8051)

    This creates a struct which has three fields: age, id and name. The corresponding values are 16, 8051 and Bob.

  2. Click Run to view the output in the OML Command window:

    In the Variable Browser, you may expand this struct by clicking the plus sign, +, in front of student to check its field and the corresponding values:

Modify a Sruct

You can add new fields or delete fields in an existing struct by using the building functions setfield and rmfield.
  1. To add a new field called score in the student struct, use the commands below:
    student.score = ‘A’

    or

    setfield(student,’score’,’A’)

    Both return this result:

  2. If you want to remove a field in an existing struct, you must use the function rmfield. For example, to remove the age field in the student struct, use the command below:
    rmfield(student,’age’)

    This returns:

Convert a Struct to a Cell

Structs can be easily transferred to a cell by calling the build-in function struct2cell. The original struct’s fields change to the number index, as shown below:
student.name = 'Bob';
student.age = 18;
student.id = 8015
% transfer a struct to a cell
struct2cell(student)

This returns:

Convert a Cell to Struct

You can convert a cell to a struct by using cell2struct. Since a struct must contain field names, you need to determine the field names before converting. See the example below:
Cell = {'Bob','Lily','Emma';20,18,25;8051,8052,8053};
Fields = {'name','age','id'};
Struct = cell2struct(Cell,Fields)

This script converts a cell to a struct with the fields name, age, and id respectively:

Click + in front of Struct in the Variable Browser to expand this struct. The Variable Browser displays this: