Main Content

search

Search for Tweets

Description

example

d = search(c,tweetquery) searches Tweets for the term tweetquery.

example

d = search(c,tweetquery,parameters) searches Tweets using web service query parameters. The Twitter® REST API defines web service query parameters.

example

d = search(c,tweetquery,QueryName1,QueryValue1,...,QueryNameN,QueryValueN) specifies web service query parameters as one or more pairs of name-value arguments.

Examples

collapse all

Use a Twitter connection object to search for Tweets.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey = 'abcdefghijklmnop123456789';
consumersecret = 'qrstuvwxyz123456789';
accesstoken = '123456789abcdefghijklmnop';
accesstokensecret = '123456789qrstuvwxyz';

c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If the StatusCode property has the value OK, the connection is successful.

c.StatusCode
ans = 

    OK

Search for Tweets using the Twitter connection object and the search term bat365.

tweetquery = 'bat365';
d = search(c,tweetquery)
d = 

  ResponseMessage with properties:

    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1×25 matlab.net.http.HeaderField]
          Body: [1×1 matlab.net.http.MessageBody]
     Completed: 0

d is a matlab.net.http.ResponseMessage object. The StatusCode property shows OK, indicating a successful HTTP request.

Access bat365® Tweets. Display the 12th Tweet.

 d.Body.Data.statuses{12}.text
ans =

    'MATLAB Control Systems Examples https://t.co/g2P86srv33'

You can search for other Tweets using the search function. To retrieve other Twitter data, use the getdata function.

Use a Twitter connection object to search for 20 Tweets. Specify the number of Tweets to retrieve as a structure.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey = 'abcdefghijklmnop123456789';
consumersecret = 'qrstuvwxyz123456789';
accesstoken = '123456789abcdefghijklmnop';
accesstokensecret = '123456789qrstuvwxyz';

c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If the StatusCode property has the value OK, the connection is successful.

c.StatusCode
ans = 

    OK

Specify the search term bat365 in the variable tweetquery. Specify 20 Tweets as a field in the structure parameters. Search for 20 Tweets using the Twitter connection object, search term tweetquery, and structure parameters.

tweetquery = 'bat365';
parameters.count = 20;
d = search(c,tweetquery,parameters)
d = 

  ResponseMessage with properties:

    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1×25 matlab.net.http.HeaderField]
          Body: [1×1 matlab.net.http.MessageBody]
     Completed: 0

d is a matlab.net.http.ResponseMessage object. The StatusCode property shows OK, indicating a successful HTTP request.

Access bat365 Tweets. Display the structure Data.

d.Body.Data
ans = 

  struct with fields:

           statuses: {20×1 cell}
    search_metadata: [1×1 struct]

The structure Data contains the field statuses. This field is a cell array of structures. Each structure in the cell array contains information about one Tweet®.

Access all 20 Tweets.

d.Body.Data.statuses{:}
ans = 

  struct with fields:

                   created_at: 'Fri Apr 28 17:51:55 +0000 2017'
                           id: 1.2345e+17
                       id_str: '123456789101112131'
                         text: 'This collection of over 400 MATLAB examples can help you with #controlsystems, Kalman filters, and more https://t.co/FWndxKLA6l…'
                    truncated: 0
                     entities: [1×1 struct]
                     metadata: [1×1 struct]
                       source: 'Twitter for iPhone'
        in_reply_to_status_id: []
    in_reply_to_status_id_str: []
          in_reply_to_user_id: []
      in_reply_to_user_id_str: []
      in_reply_to_screen_name: []
                         user: [1×1 struct]
                          geo: []
                  coordinates: []
                        place: []
                 contributors: []
             retweeted_status: [1×1 struct]
              is_quote_status: 0
                retweet_count: 34
               favorite_count: 0
                    favorited: 0
                    retweeted: 0
           possibly_sensitive: 0
                         lang: 'en'
...

The field text in each structure contains the text of one Tweet.

(These values do not represent real Twitter data.)

You can search for other Tweets using the search function. To retrieve other Twitter data, use the getdata function.

Use a Twitter connection object to search for 20 Tweets. Specify the number of Tweets to retrieve as a name-value argument.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey = 'abcdefghijklmnop123456789';
consumersecret = 'qrstuvwxyz123456789';
accesstoken = '123456789abcdefghijklmnop';
accesstokensecret = '123456789qrstuvwxyz';

c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If the StatusCode property has the value OK, the connection is successful.

c.StatusCode
ans = 

    OK

Search for 20 Tweets using the Twitter connection object, search term bat365, and name-value argument count.

tweetquery = 'bat365';
d = search(c,tweetquery,'count',20)
d = 

  ResponseMessage with properties:

    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1×25 matlab.net.http.HeaderField]
          Body: [1×1 matlab.net.http.MessageBody]
     Completed: 0

d is a matlab.net.http.ResponseMessage object. The StatusCode property shows OK, indicating a successful HTTP request.

Access bat365 Tweets. Display the structure Data.

 d.Body.Data
ans = 

  struct with fields:

           statuses: {20×1 cell}
    search_metadata: [1×1 struct]

The structure Data contains the field statuses. This field is a cell array of structures. Each structure in the cell array contains information about one Tweet.

Access all 20 Tweets.

d.Body.Data.statuses{:}
ans = 

  struct with fields:

                   created_at: 'Fri Apr 28 17:51:55 +0000 2017'
                           id: 1.2345e+17
                       id_str: '123456789101112131'
                         text: 'This collection of over 400 MATLAB examples can help you with #controlsystems, Kalman filters, and more https://t.co/FWndxKLA6l…'
                    truncated: 0
                     entities: [1×1 struct]
                     metadata: [1×1 struct]
                       source: 'Twitter for iPhone'
        in_reply_to_status_id: []
    in_reply_to_status_id_str: []
          in_reply_to_user_id: []
      in_reply_to_user_id_str: []
      in_reply_to_screen_name: []
                         user: [1×1 struct]
                          geo: []
                  coordinates: []
                        place: []
                 contributors: []
             retweeted_status: [1×1 struct]
              is_quote_status: 0
                retweet_count: 34
               favorite_count: 0
                    favorited: 0
                    retweeted: 0
           possibly_sensitive: 0
                         lang: 'en'
...

The field text in each structure contains the text of one Tweet.

(These values do not represent real Twitter data.)

You can search for other Tweets using the search function. To retrieve other Twitter data, use the getdata function.

Input Arguments

collapse all

Twitter connection, specified as a twitter object.

Tweet search term, specified as a character vector or string scalar.

Example: "bat365"

Data Types: char | string

Web service query parameters, specified as a structure. Each parameter is specified as a field in the structure. Set the field to a specific value in the structure. For example, specify the number of Tweets to retrieve:

parameters.count = 20;

The Twitter REST API defines web service query parameters that it accepts as part of an HTTP request.

Data Types: struct

Web service query parameters, specified as one or more pairs of name-value arguments. A QueryName argument is a character vector or string scalar that specifies the name of a query parameter. A QueryValue argument is a character vector or string scalar that specifies the value of the query parameter.

The Twitter REST API defines web service query parameters that it accepts as part of an HTTP request.

Example: 'count',20 retrieves 20 Tweets.

Data Types: char | string

Output Arguments

collapse all

Twitter data, returned as a matlab.net.http.ResponseMessage object.

To retrieve Twitter data, access properties in d, for example:

data = d.Body.Data
data = 
    
      struct with fields:
    
               statuses: {50×1 cell}
        search_metadata: [1×1 struct]

Continue to access the nested structure data to retrieve Twitter data. For accessing nested structures, see Access Data in Nested Structures.

Limitations

  • The Twitter REST API GET search/tweets endpoint specifies that you can retrieve up to a maximum of 100 Tweets at a time.

  • The Twitter REST API GET search/tweets endpoint specifies that you can retrieve up to 7 days of historical Tweets.

Version History

Introduced in R2017b