How to extract data from variable
42 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
In my Mathwork Analysis module, the result of
disp(data);
shown in the command window is
{"temperature"=>10.4, "windspeed"=>12.3, "winddirection"=>255.0, "weathercode"=>3, "is_day"=>1, "time"=>"2023-04-26T14:00"}
How can I extract a single value, for example "windspeed"?
1 commentaire
Christopher Stapels
le 26 Avr 2023
Can you show us more code? For example, how do you get the values into the variable 'data'? It looks like it might be a struct. If so, you can access the specific memebrs using dot notation
data.windspeed
for example
or
disp(data.windspeed)
Réponses (2)
albara
le 26 Avr 2023
You can extract the value associated with the "windspeed" key using MATLAB's built-in jsondecode function. Here's an example code snippet:
% Assume your JSON data is stored in a string variable named 'data'
data = '{"temperature":10.4, "windspeed":12.3, "winddirection":255.0, "weathercode":3, "is_day":1, "time":"2023-04-26T14:00"}';
% Decode the JSON data using jsondecode
json_data = jsondecode(data);
% Extract the windspeed value
windspeed = json_data.windspeed;
% Print the windspeed value to the command window
disp(windspeed);
Make sure that the JSON data is correctly formatted and that the keys and values are separated by : symbols.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
2 commentaires
Image Analyst
le 26 Avr 2023
What does this show:
whos data
I need to know what kind of variable it is. It doesn't look like a cell array. It looks more like a dictionary.
Does
windSpeed = data.windspeed
work?
5 commentaires
Image Analyst
le 26 Avr 2023
I'd look into correcting why it put => in there instead of : in the first place. Did you create the strings, or is it something someone else made up and posted online and you need to read and parse that data?
Communautés
Plus de réponses dans ThingSpeak Community
Voir également
Catégories
En savoir plus sur JSON Format dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!