How to extract a subset of a field from a structure while keeping respective information from the other fields?

13 次查看(过去 30 天)
I have a MATLAB structure with 20 fields. My structure is for example called Data and I only want to concentrate on five of them.
My important field is Data.Options. It has 3000x1 cells. It has patterns like "pt002", "gm010", "mde02" etc. in it.
The other field is Data.specifics. It also has 3000x1 cells, and it contains information relative to the rows in Data.Options.
I have two other fields of Data.Up and Data.Down with 3000x1 cells again and they contain information relative to the rows in Data.specifics. These two fields only store numbers like 1, 2, 3, etc.
My other field I would like to keep is Data.components. It has 1100x1 cells. It is basically the components that are found in Data.specifics. For example, if one row of Data.specifics is: A*D = C, Data.specifics contains A and D and C in different rows.
Now I would like to extract a subset of Data.Options which has patterns of "mtc03" and "yhk90" in it and based on that, I also want to keep the rows of that fields in which these patterns are found. I have prepared something like this from previous answers, but it is incomplete:
Data2= Data
Components = [Data2.Components]
Specifics = [Data2.Specifics]
Options = [Data2.Options]
Up= [Data2.Up]
Down= [Data2.Down]
%Index = How to give index for "mtc03" and "yhk90" based on Data2.Options?
Data2.Components = Components(Index);
Data2.Specifics= Specifics(Index);
Data2.Options= Options(Index);
Data2.Up= Up(Index);
Data2.Down= Down(Index);
I don't know if it is correct, or if there is any quicker way. Thanks for any help!

采纳的回答

Ive J
Ive J 2022-1-22
field components doesn't have the same size as others. Maybe it's better if you provide part of your struct at least. Anyways, you can do something like this:
% take care of capital letters first!
idx = ismember(data.options, {'yhk90', 'mtc03'});
fi = ["options", "up", "down", "components"];
for i = 1:numel(fi)
data.(fi(i))(~idx) = [];
end
end
  10 个评论
Ive J
Ive J 2022-1-22
In line with my comment, this snippet extracts a submodel from your model with reaction and mets involved in sec00740 pathway:
model = load('yeast-GEM.mat').model;
% all rxns in sce00740 pathway
idx = contains(model.rxnKEGGPathways, 'sce00740');
submodel = struct;
fi = ["rxnKEGGPathways", "rxns", "ub", "lb", "rxnKEGGID"];
for i = 1:numel(fi)
submodel.(fi(i)) = model.(fi(i))(idx);
end
% now find mets corresponding to rxns in submodel.rxns
submodel.S = model.S(:, idx);
orphanMetsIdx = sum(abs(submodel.S), 2) == 0; % remaining orphan mets in the submodel
submodel.S(orphanMetsIdx, :) = []; % remove those mets from the submodel
fi = ["mets", "metNames", "metKEGGID"]; % some desired fields for metabolites
for i = 1:numel(fi)
submodel.(fi(i)) = model.(fi(i))(~orphanMetsIdx);
end
submodel
submodel = struct with fields:
rxnKEGGPathways: {12×1 cell} rxns: {12×1 cell} ub: [12×1 double] lb: [12×1 double] rxnKEGGID: {12×1 cell} S: [34×12 double] mets: {34×1 cell} metNames: {34×1 cell} metKEGGID: {34×1 cell}

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Special Functions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by