Delete rows from a structure that contain a certain string anywhere in the field

14 Ansichten (letzte 30 Tage)
Hey everyone,
Two beginners' questions:
1) First question: I have a large structure ('structure') and I want to delete rows that contain 'AA' anywhere in the variable 'name' (example name: 'DKA011_AA1.vhdr'). I tried using
mask = ismember({structure.name}, 'AA');
structure = structure(~mask);
but 'mask' only contains zeros (probably because the fields do not only contain 'AA'). How can I delete all rows that contain AA anywhere in the text?
2) Second question: In the same structure ('structure'), I have a variable 'date' in this format '14-Apr-2016 09:51:03'. How can I transform this variable in this format: '14.4.2016 09:51'?
Any help is greatly appreciated!
Cheers,
Tobias
  1 Kommentar
dpb
dpb am 24 Okt. 2022
Bearbeitet: dpb am 24 Okt. 2022
As always, it'll be much simpler to try to write specific-application syntax for a particular data structure if we actually can see/touch the pertinent struct. For example, the use of "rows" is ambiguous here -- is this a struct array or is the field an array -- or both or neither but something else?
Attach a .mat file that has a representative sample of the subject struct
But, if one assumes is a struct array and the intent is as shown in Q? the idea is to remove those elements of the array containing the offending string, then that's simple enough --
s(contains({s.name,'AA'}))=[]; % delete those found
or
s=s(~contains({s.name,'AA'})); % keep those NOT found
NOTA BENE: the curlies "{}" around the reference to the struct field to collect the returned list into a single cell array to operate over.
2)
t=datetime('14-Apr-2016 09:51:03','format','dd.M.uuuu HH:mm');
on creation before storing or
for i=1:numel(s)
s(i).date.Format='dd.M.uuuu HH:mm';
end
if can't fix it on initial creation but must patch later. Unfortunately, there's not a way to do the assignment in an anonymous function to avoid the explicit loop construct (that I could dream up, anyway).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 24 Okt. 2022
Bearbeitet: Image Analyst am 24 Okt. 2022
I tried this and it works
% Create a structure
str = dir('*.m')
% Extract one of the string fields into a cell array.
allFilenames = {str.name}'
% Find out which structures in the structure array have the word we're looking for.
indexesToDelete = contains(allFilenames, 'demo')
% Delete items with 'demo' in the field.
str(indexesToDelete) = [];
So for you with your existing structure called "structure" and looking for _AA in the "name" field, you'd do
% Extract one of the string fields into a cell array.
allFilenames = {structure.name}'
% Find out which structures in the structure array have the word we're looking for.
indexesToDelete = contains(allFilenames, '_AA', 'IgnoreCase', true)
% Delete items with 'AA' in the field.
structure(indexesToDelete) = [];

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by