how do extract data from a multi dimensional structure array

조회 수: 16 (최근 30일)
WIlliam Main
WIlliam Main 2018년 12월 19일
댓글: AnaelG 2024년 2월 22일
I am working with Australian weather data from the web. I read my data into structure array S using JSON formatted data:
S = webread("http://www.bom.gov.au/fwo/IDQ60901/IDQ60901.95551.json" );
I can display the data in my command window by typing the desired field:
>> S.observations.data.rel_hum
ans =
58
ans =
63
ans =
69
ans =
69
etc...
But if I try to enter this data into a simple array for analysis I just get the first value:
>> RH = S.observations.data.rel_hum
RH =
58
>>
How can I get the data into simple variables for analysis and plotting?

채택된 답변

Walter Roberson
Walter Roberson 2018년 12월 19일
put [] around the whole S.whatever expression
  댓글 수: 3
Sven Baumgaartner
Sven Baumgaartner 2023년 3월 4일
Hello,
I have a similar Problem: How do you fetch the data from the second or third hour ?
I tried thinks like the following:
RH = [S.observations.data(1).rel_hum]
RH = [S.observations.data{1}.rel_hum]
But this dosent work...
Greetings from Germany
Walter Roberson
Walter Roberson 2023년 3월 4일
If you look at S.observations.data(K).local_date_time for several different K, then you can see that at least for that particular page, the observations are most recent first, and there are two observations for each hour. To locate the information for a particular day and hour, you could try something like
dt = datetime({S.observations.data.local_date_time_full}, 'InputFormat', 'yyyyMMddHHmmss');
after which you could use isbetween to determine which entries are a particular year / month / day / hour . Or you could use ymd and hms on the objects to determine the day number and the hour number (that might be an easier interface.)
Once you have a logical mask indicating which entries to select,
selected_relhum = [S.observations.data(LOGICALMASK).rel_hum];

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

추가 답변 (2개)

madhan ravi
madhan ravi 2018년 12월 19일
편집: madhan ravi 2018년 12월 19일
you may also look into arrayfun() and structfun() to plot struct array
RH = S.observations.data.rel_hum(:)
  댓글 수: 2
WIlliam Main
WIlliam Main 2018년 12월 19일
This does not appear to work, what am I doing wrong ?
>> RH = S.observations.data.rel_hum(:)
Expected one output from a curly brace or dot indexing expression, but there were 201 results.
madhan ravi
madhan ravi 2018년 12월 19일
can you upload the data as a .mat file?

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


AnaelG
AnaelG 2024년 2월 16일
More generally, I've realized that s_struct(:).data is trying to output all the contents of a cell array, like calling c_cell{:}. So if you assign it to a variable it only gives you the first element.
The trick is to assign it to each cell of a pre-declared cell array of the same size:
c_array= cell(size(s_struct));
[c_array{:}]= s_struct.data
So in your case:
RH= cell(size(S.observations.data));
[RH{:}]= S.observations.data.rel_hum
But of course if it's scalar data and you already know the type you're dealing with, you can just create the array on the right hand side:
RH= [S.observations.data.rel_hum] %if doubles
or
RH= {S.observations.data.rel_hum} %if cells
etc.
  댓글 수: 2
Stephen23
Stephen23 2024년 2월 16일
편집: Stephen23 2024년 2월 16일
"More generally, I've realized that s_struct(:).data is trying to output all the contents of a cell array"
More generally it actually returns a comma-separated list:
There are absolutely no cell arrays involved in s_struct(:).data, just a non-scalar structure:
s_struct(1).data = 1;
s_struct(2).data = 2;
iscell(s_struct)
ans = logical
0
iscell(s_struct(1).data)
ans = logical
0
iscell(s_struct(2).data)
ans = logical
0
Nope, there are absolutely no cell arrays involved anywhere in that code.
"The trick is to assign it to each cell of a pre-declared cell array of the same size"
You could do that, but by no means is that the only thing that is possible to do with comma-separated lists.
AnaelG
AnaelG 2024년 2월 22일
Stephen this is great info! I never knew there was such a thing as comma-separated lists in matlab. I just see it as an annoyance that it outputs the data as separate entries when I slice a cell array or struct or N-dim matrix...
Assigning to new variables in a for loop or as [c1,c2,...,cN] is not particularly practical or elegant so i'm glad I found the right combination of operators with [C{:}] ! It seems to work relatively universally with different data types.
Thanks for linking a tutorial, I'll save that reference for future use.

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

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by