How to extract a subset of a field from a structure while keeping respective information from the other fields?
13 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Ive J
on 22 Jan 2022
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 Comments
Ive J
on 22 Jan 2022
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
More Answers (0)
See Also
Categories
Find more on Special Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!