I am working on a Matlab project that connects with Thingsboard website. I use webread function to get the response from the server which sends information as JSON. When I send a request to get the users' information, I should get the information in the following format:
[
{
"email": "[email protected]",
"authority": "CUSTOMER_USER",
"firstName": "Davis",
"lastName": "Smith",
"name": "[email protected]"
},
"email": "[email protected]",
"authority": "CUSTOMER_USER",
"firstName": "DONALDSON",
"lastName": "ZAIK",
"name": "[email protected]"
},
]
However, the response that I get in Matlab using webread function is as follows:
4×1 struct array with fields:
email
authority
firstName
lastName
name
and when I access any field like email, it shows the emails of all the users as follows:
response = webread("serverurl");
response.email
ans =
'[email protected]'
ans =
'[email protected]'
What I want to know is how to get a specific user's information by knowing one field only. For example, I want to get the email,lastname and authority of the user Davis by knowing the first name "Davis".
I really appreciate your help in this matter.
You can use the following syntax:
response(:).firstNamelists all first names.{response(:).firstName}build a cell array of first names.Example:
{'Davis', 'DONALDSON'}strcmp({...}, 'Davis')Returns a logical array with value1where firstName equals 'Davis' and0where not equal.Example:
[0 1 0 0]is returned if onlyresponse(2).firstName='Davis'.response(strcmp...)Uses logical indices for returning a new array where index equals1.Example:
response(logical([0 1 0 0])), returns an array (with length 1) containing the second struct ofresponse.Sample code:
Result:
Now you can get any field like
filtered_response.emailin case there is only one struct withfirstName='Davis'.And
filtered_response(:).emailin case there is more than one matching struct.