Converting python code to matlab code
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
abc xyz
am 18 Jun. 2020
Kommentiert: Ameer Hamza
am 18 Jun. 2020
Can someone please help me to convert the below python code into MATLAB?
%python code
import requests
import random
import time
ENDPOINT = "api.somewebsite.com"
LABEL1 = "label1"
LABEL2 = "label2"
TOKEN = "XXXXXXXXXX"
DELAY = 1 # Delay in seconds
def post_var(payload, url=ENDPOINT, device=DEVICE_LABEL, token=TOKEN):
try:
url = "http://{}/api/v1.1/dev/{}".format(url, LABEL1)
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
attempts = 0
status_code = 400
while status_code >= 400 and attempts < 5:
print("[INFO] Sending data, attempt number: {}".format(attempts))
req = requests.post(url=url, headers=headers,
json=payload)
status_code = req.status_code
attempts += 1
time.sleep(1)
print("[INFO] Results:")
print(req.text)
except Exception as e:
print("[ERROR] Error posting, details: {}".format(e))
def main():
value = random.random() * 100
payload = {LABEL2: value}
post_var(payload)
if __name__ == "__main__":
while True:
main()
time.sleep(DELAY)
Thanks,
A confused student
3 Kommentare
KSSV
am 18 Jun. 2020
Have a look on publish. I am not sure whether this addresses your question or not.
Akzeptierte Antwort
Ameer Hamza
am 18 Jun. 2020
Bearbeitet: Ameer Hamza
am 18 Jun. 2020
See webwrite(): https://www.mathworks.com/help/matlab/ref/webwrite.html
and this link: https://www.mathworks.com/help/mps/restful-api-and-json.html
8 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!