how do extract data from a multi dimensional structure array

19 ビュー (過去 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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by