How to select specific data from a structure

Hi, I have the following structure: data.variation_#.ts
- data has 23 variation fields (i.e. variation_1, variation_2 etc.), and I'm interested in field 3 to 23
- The ts field is a 30x10800 double
How can I read throug the variation fields of interest(3 to 23) and for all of them get some of the rows from ts (for example rows 2 3 4, all columns)
Thanks

1 Kommentar

dpb
dpb am 21 Jun. 2016
"... following structure: data.variation#.ts"_
Bad idea here is your basic problem. Instead of sequentially-named fields of 2D array, use 3D array where each variation is a plane. Then you simply refer to the planes of interest.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Guillaume
Guillaume am 22 Jun. 2016

3 Stimmen

This is why I think it's not a good idea to recommend using dynamic field names instead of eval, it leads to the same problems.
If you have numbered variables or field names, you have a poor data structure and it indeed becomes difficult to operate the same process on all of them at once. The proper solution is to get rid of all these numbered variables / field names and store their content into a cell array (if the size of the content varies from variable to variable) or a matrix (if not). It is then trivial to index them.
See Jos' answer for a good structure. However, personally I would avoid the multilevel structure, it's a pain to work with (See my comment to Jos' answer).
Assuming that the variation fields are the only field of data, you can convert your existing structure into Jos' suggestion with:
newdata.variation = cell2mat(struct2cell(data))
%or you could just do
variation = cell2mat(struct2cell(data))
%and not bother with multilevel structures
To then get rows [3 4 8] of variations 3 to 23 as a 3d array:
selectedts = cat(3, variation(3:23).ts);
selectedrows = selectedts([3 4 8], :, :);

2 Kommentare

Isma_gp
Isma_gp am 22 Jun. 2016
Hei, the variation fields are not the only fields of data. Is there any way to convert the data then?
Thanks
Probably, the safest way to do this:
wantedfields = sprintfc('variation_%d', 3:23);
[~, fieldrows] = ismember(wantedfields, fieldnames(data));
dataascell = struct2cell(data);
variations = cell2mat(dataascell(fieldrows));
selectedts = cat(3, variation(3:23).ts);
selectedrows = selectedts([3 4 8], :, :);

Melden Sie sich an, um zu kommentieren.

Jos (10584)
Jos (10584) am 22 Jun. 2016

2 Stimmen

Store your data like this
data.variation(1).values = ..
data.variation(2).values = ..
data.variation(3).values = ..
which makes it trivial to select the fields you need
data.variation([3 4 8])
It is the content of a variable (or field) that is supposed to be flexible, not its name!

3 Kommentare

Guillaume
Guillaume am 22 Jun. 2016
Bearbeitet: Guillaume am 22 Jun. 2016
Totally agree with "It is the content of a variable (or field) that is supposed to be flexible, not its name!"
The proposed solution however does not work as is, since
data.variation([3 4 8]).values
will return a comma-separated list. Some extra machinery is required, either:
cat(x, data.variation([3 4 8]).values); %where x is ndims(values)+1
if all the values have the same size or
{data.variation([3 4 8].values}
if not.
Jos (10584)
Jos (10584) am 23 Jun. 2016
Nice follow-up, Guillaume.
Shahram
Shahram am 16 Dez. 2022
Thanks "data.variation([3 4 8])" worked wel

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 21 Jun. 2016

Kommentiert:

am 16 Dez. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by