私はThingsboardWebサイトに接続するMatlabプロジェクトに取り組んでいます。webread関数を使用して、情報をJSONとして送信するサーバーから応答を取得します。ユーザーの情報を取得するリクエストを送信すると、次の形式で情報を取得する必要があります。
[
{
"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]"
},
]
ただし、webread関数を使用してMatlabで取得する応答は次のとおりです。
4×1 struct array with fields:
email
authority
firstName
lastName
name
電子メールなどのフィールドにアクセスすると、すべてのユーザーの電子メールが次のように表示されます。
response = webread("serverurl");
response.email
ans =
'[email protected]'
ans =
'[email protected]'
私が知りたいのは、1つのフィールドだけを知って特定のユーザーの情報を取得する方法です。たとえば、「Davis」という名を知って、ユーザーDavisの電子メール、姓、および権限を取得したいとします。
この件に関してあなたの助けに本当に感謝します。
次の構文を使用できます。
filtered_response = response(strcmp({response(:).firstName}, 'Davis'));
response(:).firstName
すべての名を一覧表示します。 {response(:).firstName}
名のセル配列を作成します。{'Davis', 'DONALDSON'}
strcmp({
... firstNameが 'Davis'に等しく、等しくない}, 'Davis')
値を持つ論理配列を返します。例:=のみの場合に返されます。1
0
[0 1 0 0]
response(2).firstName
'Davis'
response(strcmp
...)
論理インデックスを使用して、インデックスがに等しい新しい配列を返し1
ます。response(logical([0 1 0 0]))
、は、の2番目の構造体を含む配列(長さ1)を返しますresponse
。サンプルコード:
%Build an array containing two structures (just for the example)
%Assume response is the result of webread
response = [struct('email', '[email protected]', 'authority', 'CUSTOMER_USER', 'firstName', 'Davis', 'lastName', 'Smith', 'name', '[email protected]');...
struct('email', '[email protected]', 'authority', 'CUSTOMER_USER', 'firstName', 'DONALDSON', 'lastName', 'ZAIK', 'name', '[email protected]')];
filtered_response = response(strcmp({response(:).firstName}, 'Davis'));
結果:
filtered_response =
struct with fields:
email: '[email protected]'
authority: 'CUSTOMER_USER'
firstName: 'Davis'
lastName: 'Smith'
name: '[email protected]'
これでfiltered_response.email
、firstName
=を持つ構造体が1つしかない場合のように、任意のフィールドを取得できます'Davis'
。
またfiltered_response(:).email
、一致する構造体が複数ある場合。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加