cell2struct - Convert cell array to structure array - MATLAB (original) (raw)
Convert cell array to structure array
Syntax
Description
`structArray` = cell2struct([cellArray](#mw%5Fab1036fc-5c98-4d4b-bbc3-87fa4725b4f6),[fields](#mw%5F2544b197-b229-4047-99bc-47b88ae293a0))
creates a structure array from the information contained in the cell array and using the specified field names. The function assigns each row of the cell array to the corresponding field name in the structure.
`structArray` = cell2struct([cellArray](#mw%5Fab1036fc-5c98-4d4b-bbc3-87fa4725b4f6),[fields](#mw%5F2544b197-b229-4047-99bc-47b88ae293a0),[dim](#mw%5F4cdd72de-f89f-4940-b9f9-bc168b9f72c4))
assigns the data sets along the dimension dim
of the cell array to the corresponding field name in the structure. For example, specify 2 fordim
to assign each column of the cell array to the corresponding field name.
Examples
Create a cell array that stores high and low temperatures for five days.
T(1,:) = {"highs",[44,38,46,43,48]}; T(2,:) = {"lows",[35,31,32,28,35]}
T=2×2 cell array {["highs"]} {[44 38 46 43 48]} {["lows" ]} {[35 31 32 28 35]}
Use the first column of data in the cell array to define field names for the structure.
fields = 1×2 string "highs" "lows"
Copy the temperature data from the second column of the cell array into a cell array named temps
.
temps=2×1 cell array {[44 38 46 43 48]} {[35 31 32 28 35]}
Call cell2struct
with the temps
cell array and field names as inputs. MATLAB assigns each row of data from temps
to the corresponding field name.
Tstruct = cell2struct(temps,fields)
Tstruct = struct with fields: highs: [44 38 46 43 48] lows: [35 31 32 28 35]
Index into the structure to get the low temperature for the third day.
Create a cell array that contains anonymous patient information, including an ID number, body temperature, and blood pressure.
patients(1,:) = {"A134",98.5,[124 85]}; patients(2,:) = {"B267",99.1,[109 77]}; patients(3,:) = {"C889",97.9,[120 81]}
patients=3×3 cell array {["A134"]} {[98.5000]} {[124 85]} {["B267"]} {[99.1000]} {[109 77]} {["C889"]} {[97.9000]} {[120 81]}
Create a vector of field names to use for the struct.
fields = ["PatientID","BodyTemp","BloodPressure"];
To associate each column of data in the cell array with the corresponding field name in the structure, call cell2struct
with the dimension set to 2.
patientStruct = cell2struct(patients,fields,2)
patientStruct=3×1 struct array with fields: PatientID BodyTemp BloodPressure
Retrieve the blood pressure of patient C889 in the newly created structure array.
patientC889BP = patientStruct(3).BloodPressure
patientC889BP = 1×2
120 81
Input Arguments
The cell array to be converted to a structure array.
The names of the structure array fields, specified as a string array or cell array of character vectors. The number of field names must match the number of the number of cells along the dimension specified by dim.
The dimension along which to split the cell array, specified as a positive integer. For example, the default value of 1
assigns each row of the cell array to the corresponding field name in fields. Specify2
to assign each column of the cell array to the corresponding field name.
Extended Capabilities
Usage notes and limitations:
- The input arguments
fields
anddim
must be compile-time constants.
Version History
Introduced before R2006a