Delete fields of struct based on starting characters
1 view (last 30 days)
Show older comments
Hello everyone. I have a struct called temp which has 5 fields

I want to delete all the fields starting with 'fig' and 'ax'. The final output struct should look something like

I know that we can do it in a complex way using fieldnames and rmfield with multiple lines of code.
Is there any simple way to do this? Thanks in advance.
0 Comments
Accepted Answer
Guillaume
on 26 Feb 2020
Edited: Guillaume
on 26 Feb 2020
You do have to use fieldnames and rmfield indeed. It's only two lines and not complicated at all:
fns = fieldnames(temp);
temp = rmfield(temp, fns(startsWith(fns, {'fig', 'ax'})))
edit: However, note that if you're trying to remove all the graphics objects from your structure a more reliable method would be:
fns = fieldnames(temp);
temp = rmfield(temp, fns(cellfun(@(fn) ishghandle(temp.(fn)), fns)));
More Answers (0)
See Also
Categories
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!