How do I read the last 3 Thinkspeak channel statuses into an array?

5 ビュー (過去 30 日間)
Chris
Chris 2025 年 5 月 9 日
コメント済み: Chris 2025 年 5 月 9 日
I am currently running a ThingSpeak Maltlab analysis that reads the latest channel status and acts on it. I am using this code, which works fine:
url='https://api.thingspeak.com/channels/mychan/feeds/last.json?api_key=mykey&status=true';
lastData=webread(url);
status = [lastData.status];
I am trying to figure out how to change this to read the last 3 channel statuses and put them into an array like status(i) where i goes from 1-3? This url returns the 3 statuses I want:
url = https:'//api.thingspeak.com/channels/mychan/status.json?api_key=mykey&results=3'
But I can't figure out how to get them into an array to work with. Does anyone know how to do this? Thanks.
  2 件のコメント
Image Analyst
Image Analyst 2025 年 5 月 9 日
編集済み: Image Analyst 2025 年 5 月 9 日
Does anything in the url change when you "read the last 3 channel"? Like mychan or something? Or do you call the same URL, just in a loop after a time delay? Like
for timePoint = 1 : 3
lastData=webread(url);
status(timePoint) = lastData.status;
pause(2); % Pause 2 seconds.
end
What is the lastData variable? What does this show
lastData
whos lastData
Chris
Chris 2025 年 5 月 9 日
No, the channel and the key are identical. Here are the results I get if I use those urls in a browser (first for the last status, then for the last 3):
{
"created_at": "2025-05-09T14:07:20Z",
"entry_id": 1998612,
"field1": "-0.04087",
"field2": "506",
"field3": "0",
"field4": "0",
"field5": "1008",
"field6": "0",
"field7": "464",
"field8": "0",
"status": "000000"
}
When querried for last 3 statuses:
{
"channel": {
"name": "Boiler 2",
"latitude": "4x,xx",
"longitude": "-7x.xx",
"elevation": "11"
},
"feeds": [
{
"created_at": "2025-05-09T14:07:35Z",
"entry_id": 1998613,
"status": "000000"
},
{
"created_at": "2025-05-09T14:09:35Z",
"entry_id": 1998614,
"status": "000000"
},
{
"created_at": "2025-05-09T14:11:35Z",
"entry_id": 1998615,
"status": "000000"
}
]
}

サインインしてコメントする。

採用された回答

Christopher Stapels
Christopher Stapels 2025 年 5 月 9 日
編集済み: Christopher Stapels 2025 年 5 月 9 日
url = 'https://api.thingspeak.com/channels/mychan/status.json?api_key=mykey&results=3';
data = webread(url);
statusArray = {data.feeds.status};
Gives you a cell array of statuses (statusi?) :).
Full Disclosure: I did use a little Gen AI to help me on this one.
  3 件のコメント
Christopher Stapels
Christopher Stapels 2025 年 5 月 9 日
Have you tried the MATLAB AI Chat playground? Its uses MATLAB documentation, so it tends to be a little more focused on the MATLAB language.
Chris
Chris 2025 年 5 月 9 日
No, just Google's AI. I'll have to check this one out; I wasn't aware of it. Thanks again.

サインインしてコメントする。

その他の回答 (0 件)

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeWrite Data to Channel についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by