フィルターのクリア

change the element-by-element organization into a plane organization

2 ビュー (過去 30 日間)
Claudia
Claudia 2012 年 5 月 15 日
Hello
I have an array <20000x1 struct> in element-by-element organization. Each of the 20000 entries has more than 30 fields.
I would like to change the element-by-element organization into a plane organization.
I found how to do it from plane to element-by-element ( http://www.mathworks.com/matlabcentral/answers/4581-deleting-the-i-th-entry-from-all-fields-of-a-struct) but I cannot figure out how to do it in the opposite way.
Could you help me, please?
Thank you very much
Claudia
  1 件のコメント
Claudia
Claudia 2012 年 11 月 8 日
long time ago I found my own solution but forgot to put it here. So, here it is:
folder = ['/scratch ...']
fields = fieldnames (datastructold) ;
for f = 1:size(fields,1) ;
varname = genvarname(fields{f}) ;
extract = {datastructold.(varname)};
datastructnew.(varname) = extract ;
end
v = genvarname([ 'name' ]) % name for variable
S.(v) = datastructnew
save([ folder v '.mat' ] , '-struct' , 'S' ) ; % name for .mat

サインインしてコメントする。

採用された回答

Claudia
Claudia 2013 年 1 月 29 日
long time ago I found my own solution but forgot to put it here. So, here it is:
folder = ['/scratch ...']
fields = fieldnames (datastructold) ;
for f = 1:size(fields,1) ;
varname = genvarname(fields{f}) ;
extract = {datastructold.(varname)};
datastructnew.(varname) = extract ;
end
v = genvarname([ 'name' ]) % name for variable
S.(v) = datastructnew
save([ folder v '.mat' ] , '-struct' , 'S' ) ; % name for .mat

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 5 月 15 日
struct2cell(), then either cell2mat() or expand the cell within a vertcat() or horzcat().
However, if the fields are not all exactly the same size or are of mixed numeric type involving more than one integer data type, then you are going to have problems.
  1 件のコメント
Claudia
Claudia 2012 年 5 月 18 日
Hi Walter
Thank you for your answer. I tried what you suggested.
But for cell2mat all contents of the input cell array must be of the same data type.
And vertcat() and horzcat() did not change anything at all.
So, at the end I missed the step to get the structure with plane organization.
It would be nice if you could explain that step.
Thanks, Claudia

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2012 年 11 月 8 日
編集済み: Sean de Wolski 2012 年 11 月 8 日
So if this is a simple example of your struct:
S = struct('field1',num2cell(1:10),'field2',num2cell(repmat(pi,1,10)))
Then I would recommend using:
S2 = squeeze(struct2cell(S))
To now have a 2x10 cell array.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by