how do extract data from a multi dimensional structure array
20 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Walter Roberson
on 19 Dec 2018
put [] around the whole S.whatever expression
3 Comments
Sven Baumgaartner
on 4 Mar 2023
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
on 4 Mar 2023
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];
More Answers (2)
madhan ravi
on 19 Dec 2018
Edited: madhan ravi
on 19 Dec 2018
you may also look into arrayfun() and structfun() to plot struct array
RH = S.observations.data.rel_hum(:)
2 Comments
AnaelG
on 16 Feb 2024
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 Comments
Stephen23
on 16 Feb 2024
Edited: Stephen23
on 16 Feb 2024
"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)
iscell(s_struct(1).data)
iscell(s_struct(2).data)
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
on 22 Feb 2024
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.
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!