how do extract data from a multi dimensional structure array

21 vues (au cours des 30 derniers jours)
WIlliam Main
WIlliam Main le 19 Déc 2018
Commenté : AnaelG le 22 Fév 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?

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Déc 2018
put [] around the whole S.whatever expression
  3 commentaires
Sven Baumgaartner
Sven Baumgaartner le 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
Walter Roberson le 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];

Connectez-vous pour commenter.

Plus de réponses (2)

madhan ravi
madhan ravi le 19 Déc 2018
Modifié(e) : madhan ravi le 19 Déc 2018
you may also look into arrayfun() and structfun() to plot struct array
RH = S.observations.data.rel_hum(:)
  2 commentaires
WIlliam Main
WIlliam Main le 19 Déc 2018
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 le 19 Déc 2018
can you upload the data as a .mat file?

Connectez-vous pour commenter.


AnaelG
AnaelG le 16 Fév 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 commentaires
Stephen23
Stephen23 le 16 Fév 2024
Modifié(e) : Stephen23 le 16 Fév 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)
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 le 22 Fév 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures 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!

Translated by