Converting a structure into a cell array in Octave
In this lesson, I will explain how to convert a structure to an array of cells in Octave using the function struct2cell().
Let me provide you with a practical example.
Create a structure that contains arrays.
C = struct('exam',{'Math', 'Latin', 'Science'},'grade',{97,60,78})
This is a structure of arrays with two fields (subject and grade) and three records within it.
Exam | grade |
---|---|
Math | 97 |
Latin | 60 |
Science | 78 |
To convert the structure into a cell array, use the function struct2cell()
The function takes only one parameter, the variable that contains the structure of arrays.
Type D=struct2cell(C)
>> D=struct2cell(C);
The variable D creates an array of cells containing data from the structure.
For instance, type D(1,1,:)(:) to view the data of the first row of the cell array.
>> D(1,1,:)(:)
ans =
{
[1,1] = Math
[2,1] = Latin
[3,1] = Science
}
Type D(2,1,:)(:) to view the data in the second row of the cell array.
>> D(2,1,:)(:)
ans =
{
[1,1] = 97
[2,1] = 60
[3,1] = 78
}
This method allows you to convert any structure into a cell array in Octave.