How to select specific data from a structure

조회 수: 147 (최근 30일)
Isma_gp
Isma_gp 2016년 6월 21일
댓글: Shahram 2022년 12월 16일
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
dpb
dpb 2016년 6월 21일
"... 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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2016년 6월 22일
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
Isma_gp
Isma_gp 2016년 6월 22일
Hei, the variation fields are not the only fields of data. Is there any way to convert the data then?
Thanks
Guillaume
Guillaume 2016년 6월 22일
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], :, :);

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2016년 6월 22일
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
Jos (10584)
Jos (10584) 2016년 6월 23일
Nice follow-up, Guillaume.
Shahram
Shahram 2022년 12월 16일
Thanks "data.variation([3 4 8])" worked wel

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by