how do extract data from a multi dimensional structure array
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
WIlliam Main
am 19 Dez. 2018
Kommentiert: AnaelG
am 22 Feb. 2024
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 19 Dez. 2018
put [] around the whole S.whatever expression
3 Kommentare
Sven Baumgaartner
am 4 Mär. 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
am 4 Mär. 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];
Weitere Antworten (2)
madhan ravi
am 19 Dez. 2018
Bearbeitet: madhan ravi
am 19 Dez. 2018
you may also look into arrayfun() and structfun() to plot struct array
RH = S.observations.data.rel_hum(:)
2 Kommentare
AnaelG
am 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 Kommentare
Stephen23
am 16 Feb. 2024
Bearbeitet: Stephen23
am 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
am 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.
Siehe auch
Kategorien
Mehr zu Structures 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!