Is it possible: Split a structure?

60 views (last 30 days)
Zoe
Zoe on 15 Jun 2011
I have a structure, Memb
Memb =
NAME: {963x1 cell}
INDUSTRY_SECTOR: {963x1 cell}
PRICE: [963x1 double]
VOLUME: [963x1 double]
Memb is already sorted by INDUSTRY_SECTOR (totally 9 sectors, Eneragy, Finance...).
Now I'd like to split the structure Memb into 9 individual structures according to which sector they belones to. Is it possible? Could any one provide a simple way to code?? (Avoid using loop if possible)
Any suggestions are greatly appreciate. Thanks a lot!!!!!!!!

Accepted Answer

the cyclist
the cyclist on 15 Jun 2011
>> [isInOneOfTheListedSectors,indexToWhichSector] = ismember([Memb.INDUSTRY_SECTOR],{'Energy';'Finance'});

More Answers (3)

Fangjun Jiang
Fangjun Jiang on 15 Jun 2011
Not sure if it is too late but it would be easier for coding if your data is stored in a structure array, instead of a structure. For example:
Memb =
NAME: {'a' 'b' 'c'}
INDUSTRY: {'ENG' 'ENG' 'FIN'}
PRICE: [1 2 3]
VOLUMN: [100 200 300]
Do the following:
S=struct('Name',Memb.NAME,'INDUSTRY',Memb.INDUSTRY,'PRICE',mat2cell(Memb.PRICE,1,ones(1,3)),'VOLUMN',mat2cell(Memb.VOLUMN,1,ones(1,3)))
You'll get
S =
1x3 struct array with fields:
Name
INDUSTRY
PRICE
VOLUMN
>> S(1)
ans =
Name: 'a'
INDUSTRY: 'ENG'
PRICE: 1
VOLUMN: 100
Then
IndustryName=unique({S.INDUSTRY})
ENG_index=ismember({S.INDUSTRY},'ENG')
ENG_data=S(ENG_index)
ENG_data =
1x2 struct array with fields:
Name
INDUSTRY
PRICE
VOLUMN
Note that in my example, Memb.NAME is a row vector. Your Memb.NAME is a column vector. So if you decide to convert to structure array, you need to use:
S=struct('Name',Memb.NAME,'INDUSTRY_SECTOR',Memb.INDUSTRY_SECTOR,'PRICE',mat2cell(Memb.PRICE,ones(963,1),1),'VOLUMN',mat2cell(Memb.VOLUMN,ones(963,1),1))
  1 Comment
Zoe
Zoe on 15 Jun 2011
Never too late. Thanks, very detailed and helpful:)

Sign in to comment.


Walter Roberson
Walter Roberson on 15 Jun 2011
INDUSTRY_SECTOR is a cell array of strings? If so, then ismember() against the valid list; the second output of ismember will be the index of the sector. After that probably the easiest would be a loop over the number of sectors to do the actual splitting.
  1 Comment
Zoe
Zoe on 15 Jun 2011
Yes, INDUSTRY_SECTOR is a cell array of strings. Tnahks :)

Sign in to comment.


Zoe
Zoe on 15 Jun 2011
xInds = strcmp(Memb.INDUSTRY_SECTOR,'Basic Materials');
That works too!

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!