Main Content

setvartype

Set data types used to import and export variables to stream

Since R2022b

    This function requires Streaming Data Framework for MATLAB® Production Server™.

    Description

    example

    opts = setvartype(opts,selection,type) sets the data types of all variables in the object opts to the data type specified by type.

    • If opts is an ImportOptions object, then setvartype returns an ImportOptions object.

    • If opts is an ExportOptions object, then setvartype returns an ExportOptions object.

    opts = setvartype(opts,selection,type) sets the data types of the variables specified by selection to the data types specified by type in the object opts.

    Examples

    collapse all

    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

    collapse all

    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.

    DataMATLAB Data Type
    Text

    "char" or "string"

    Numeric

    "single", "double", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", or "uint64"

    Undefined floating-point numbers NaN, -Inf, +Inf are only valid for single and double data types. Therefore, when you change the type of floating-point data to an integer, the importing/exporting function converts the undefined floating-point numbers to valid integers. For example, when converting to the "uint8" data type:

    • NaN is converted to 0.

    • -Inf is converted to intmin("int8").

    • +Inf is converted to intmax("int8").

    The same conversion process applies to all the integer data types: int8, int16, int16, int32, int64, uint8, uint16, uint32, or uint64.

    Logical

    "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