How to extract data from variable

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

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)

댓글을 달려면 로그인하십시오.

답변 (2개)

albara
albara 2023년 4월 26일

1 개 추천

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

Manfred
Manfred 2023년 4월 26일
I get the data by an webread command. There are "=>" charcters instead of ":" in the string. If i exchange them, it work like you explained.
Is there an webread option that might solve this problem?
Manfred
Manfred 2023년 4월 26일
편집: Manfred 2023년 4월 26일
I solved the problem myself by the strrep function
data = jsondecode(strrep(data,'=>',':'));
Thank you for your helpful response.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2023년 4월 26일

1 개 추천

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

Manfred
Manfred 2023년 4월 26일
Does
windSpeed = data.windspeed
work?
No,
Error-Message: "Error using jsondecode
JSON syntax error at line 1, column 15 (character 15): expected ':' but found '='.
Error in open-meteo Temperatur (line 36)
analyzedData = jsondecode(data);"
There is a "=>" instead of ":" in the data string.
Image Analyst
Image Analyst 2023년 4월 26일
OK it looks like it's a JSON thing, though you didn't show the result of the whos command like I asked. So you should accept @albara's answer.
If his Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
Manfred
Manfred 2023년 4월 26일
Is there an webread option that might solve this problem?
Image Analyst
Image Analyst 2023년 4월 26일
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?
Manfred
Manfred 2023년 4월 26일
편집: Manfred 2023년 4월 26일
I solved the problem myself by the strrep function
data = jsondecode(strrep(data,'=>',':'));
Thank you for your helpful response.

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2023년 4월 26일

댓글:

2023년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by