struct2dataset
(Not Recommended) Convert structure array to dataset array
The dataset data type is not recommended. To work with heterogeneous data,
use the MATLAB®
table data type instead. See MATLAB
table documentation for more information.
Description
Examples
Convert a scalar structure array to a dataset array using the default options.
Create a structure array to convert.
S.Name = {'CLARK';'BROWN';'MARTIN'};
S.Gender = {'M';'F';'M'};
S.SystolicBP = [124;122;130];
S.DiastolicBP = [93;80;92];
SS = struct with fields:
Name: {3×1 cell}
Gender: {3×1 cell}
SystolicBP: [3×1 double]
DiastolicBP: [3×1 double]
The scalar structure array has four fields, each with three rows.
Convert the structure array to a dataset array.
ds = struct2dataset(S)
ds =
Name Gender SystolicBP DiastolicBP
{'CLARK' } {'M'} 124 93
{'BROWN' } {'F'} 122 80
{'MARTIN'} {'M'} 130 92
The structure field names in S become the variable names in the output dataset array. The size of ds is 3-by-4.
Convert a nonscalar structure array to a dataset array, using one of the structure fields for observation names.
Create a nonscalar structure array to convert.
S(1,1).Name = 'CLARK'; S(1,1).Gender = 'M'; S(1,1).SystolicBP = 124; S(1,1).DiastolicBP = 93; S(2,1).Name = 'BROWN'; S(2,1).Gender = 'F'; S(2,1).SystolicBP = 122; S(2,1).DiastolicBP = 80; S(3,1).Name = 'MARTIN'; S(3,1).Gender = 'M'; S(3,1).SystolicBP = 130; S(3,1).DiastolicBP = 92; S
S=3×1 struct array with fields:
Name
Gender
SystolicBP
DiastolicBP
This is a 3-by-1 structure array with 4 fields.
Convert the structure array to a dataset array, using the Name field for observation names.
ds = struct2dataset(S,'ReadObsNames','Name')
ds =
Gender SystolicBP DiastolicBP
CLARK {'M'} 124 93
BROWN {'F'} 122 80
MARTIN {'M'} 130 92
The size of ds is 3-by-3 because the structure field Name is used for observation names, and not as a dataset array variable.
ds.Properties.DimNames
ans = 1×2 cell
{'Name'} {'Variables'}
ds.Properties.ObsNames
ans = 3×1 cell
{'CLARK' }
{'BROWN' }
{'MARTIN'}
Input Arguments
Input structure array to convert to a dataset array, specified as a scalar structure array with N fields, each with M rows, or a nonscalar M-by-1 structure array with N fields.
Data Types: struct
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
Example: 'ReadObsNames','myField' specifies that the structure
field, myField, contains observation names.
Name of structure field containing observation names for the output
dataset array, specified as the comma-separated pair consisting of
'ReadObsNames' and a character vector or string
scalar containing a field name from the input structure array,
S. When you specify a field name,
struct2dataset uses that field to create
observation names, and sets ds.Properties.DimNames
equal to {ReadObsNames,'Variables'}.
For example, to specify that observation names are in the structure
field, Names, use
Example: 'ReadObsNames','Names'
By default, or if ReadObsNames is equal to
false, struct2dataset does not
create observation names unless you specify names using the name-value
pair argument ObsNames.
Observation names for the output dataset array, specified as the
comma-separated pair consisting of 'ObsNames' and a
string array or cell array of character vectors containing observation
names. The names do not need to be valid MATLAB identifiers, but they must be unique.
Indicator for how to treat a scalar input structure array, specified
as the comma-separated pair consisting of 'AsScalar'
and either true or false. The
default value is true if S is a
scalar structure array, and false otherwise.
By default, struct2dataset converts a scalar
structure array with N fields, each with
M rows, into an
M-by-N dataset array.
If instead you set AsScalar equal to
false for a scalar input structure array, then
struct2dataset converts S to a
dataset array with N observations.
Output Arguments
Output dataset array, returned by default with M observations and N variables.
If
Sis a scalar structure array with N fields, each with M rows, thendsis an M-by-N dataset array.If
Sis a nonscalar M-by-1 structure array with N fields, thendsis an M-by-N dataset array.If
Sis a scalar structure array with N fields, each with M rows, andAsScalaris set equal tofalse, thendsis a dataset array with N observations.
Version History
Introduced in R2012b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)