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

10 views (last 30 days)
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!

Accepted Answer

Ive J
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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!