setvartype
This function requires Streaming Data Framework for MATLAB® Production Server™.
Description
opts = setvartype(
sets the data types of all variables in the object opts,selection,type)opts to the data
type specified by type.
If
optsis anImportOptionsobject, thensetvartypereturns anImportOptionsobject.If
optsis anExportOptionsobject, thensetvartypereturns anExportOptionsobject.
opts = setvartype(
sets the data types of the variables specified by opts,selection,type)selection to the data
types specified by type in the object opts.
Examples
Assume that you have a Kafka® server running at the network address kafka.host.com:9092
that has a topic CoolingFan.
Create an object to connect to Kafka streaming data.
ks = kafkaStream("kafka.host.com",9092,"CoolingFan")
Create an import options object from the KafkaStream object.
io = detectImportOptions(ks)
io =
ImportOptions with properties:
VariableNames: ["vMotor" "wMotor" "Tmass" … ]
VariableTypes: ["double" "double" "double" … ]
KeyVariable: "key"
SelectedVariableNames: ["vMotor" "wMotor" "Tmass" … ]Examine data types of the variables.
disp([io.VariableNames' io.VariableTypes'])
"vMotor" "double"
"wMotor" "double"
"Tmass" "double"
"ExternalTempAnomaly" "double"
"FanDragAnomaly" "double"
"VoltageSourceAnomaly" "double"
"FanRow" "double"
"FanColumn" "double"
"FanID" "double"
"GroupID" "double"
"key" "string"Change the data types of vMotor and wMotor
variables to int32.
io = setvartype(io,{"vMotor","wMotor"},"int32")io =
ImportOptions with properties:
VariableNames: ["vMotor" "wMotor" "Tmass" … ]
VariableTypes: ["int32" "int32" "double" … ]
KeyVariable: "key"
SelectedVariableNames: ["vMotor" "wMotor" "Tmass" … ]Import the variables with their updated types using readtimetable.
tt = readtimetable(ks,io);
Alternatively, you can set the ImportOptions property of the
stream object and the use
readtimetable.
ks.ImportOptions = io; tt = readtimetable(ks);
Assume that you have a Kafka server running at the network address kafka.host.com:9092
that has the topics Triangles and
numericTriangles.
Create a KafkaStream object connected to the Triangles topic.
inKS = kafkaStream("kafka.host.com",9092,"Triangles");
Read events from the Triangles topic into a timetable. Preview the
data by viewing the first row. The a, b, and
c triangle side lengths are stored as strings.
tt = readtimetable(inKS); row = tt(1,:)
row =
1×3 timetable
timestamp a b c
___________ ____ ____ ____
03-Sep-2022 "15" "31" "36"Use detectExportOptions to generate an
ExportOptions object from the Kafka stream object. The function obtains the types used to export the variables
from the first row of the timetable.
opts = detectExportOptions(inKS,row);
Use getvartype to confirm that the side length variables are
currently exported to the stream as strings.
type = getvartype(opts,["a" "b" "c"]);
type =
1×3 string array
"string" "string" "string"Update the export options so that the side lengths are exported as
double values. Confirm the updated options by using
getvartype.
opts = setvartype(opts,["a","b","c"],"double"); [name,type] = getvartype(opts); fprintf("%s: %s\n", [name; type])
a: double b: double c: double
Connect to the stream to export data to numericTriangles.
outKS = kafkaStream("kafka.host.com",9092,"numericTriangles", ... ExportOptions=opts)
outKS =
KafkaStream with properties:
Topic: "numericTriangles"
Group: "85c42e39-695d-467a-86f0-f0095792e7de"
Order: EventTime
Host: "kafka.host.com"
Port: 9092
ConnectionTimeout: 30
RequestTimeout: 61
ImportOptions: "None"
ExportOptions: "Source: string"
PublishSchema: "true"
WindowSize: 50
KeyVariable: "key"
KeyEncoding: "utf16"
KeyType: "text"
KeyByteOrder: "BigEndian"
BodyEncoding: "utf8"
BodyFormat: "JSON"
ReadLimit: "Size"
TimestampResolution: "Milliseconds"
Export the timetable to the new stream. The triangle side lengths in this stream are
of type double.
writetimetable(outKS,tt);
Input Arguments
Event stream options, specified as an ImportOptions or ExportOptions object. The opts object contains properties
that control the data import/export process, such as variable names and types.
Selected variables, specified as a character vector, string scalar, cell array of character vectors, or string array.
Variable names must be a subset of the names recognized by the
opts object.
Example: 'FanID'
Example: "FanID"
Example: {'FanID','vMotor'}
Example: ["FanID" "vMotor"]
Data Types: char | string | cell
New data type of variable, specified as a string scalar containing a valid
MATLAB data type name. The variable type designates the data
type to use when importing or exporting the variable. Use one of the data types listed
in this table.
| Data | MATLAB Data Type |
|---|---|
| Text |
|
| Numeric |
Undefined floating-point numbers
The same conversion process applies to all the integer data
types: |
| Logical |
|
Example: io = setvartype(io,"vMotor","int32") changes the data
type of the event stream variable vMotor to
int32.
Data Types: string
Version History
Introduced in R2022b
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)