Main Content

getvartype

Data types used to export variables to stream

Since R2022b

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

    Description

    example

    type = getvartype(opts) returns the data types of variables when they are exported to an event stream using the export options specified by opts.

    example

    [name,type] = getvartype(opts) also returns the names of the variables.

    example

    ___ = getvartype(opts,selection) returns the data types, and optionally the names, only for the variables specified by selection.

    Examples

    collapse all

    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 export options, specified as an ExportOptions object.

    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

    Output Arguments

    collapse all

    Data types of variables exported to the stream, returned as a string array.

    Each element of type specifies the data type of a variable in the stream. If you specified selection, the order of the returned types matches the order of the variables named in selection. Otherwise, the order matches the order of the variable names in the timetable row used to create opts in the call to detectExportOptions.

    Names of variables exported to the stream, returned as a string array.

    Each element of name specifies the name of a variable in the stream. The number and order of the returned names matches the number and order of the data types returned by type.

    Version History

    Introduced in R2022b