Delete fields of struct based on starting characters
2 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone. I have a struct called temp which has 5 fields
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273813/image.png)
I want to delete all the fields starting with 'fig' and 'ax'. The final output struct should look something like
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273814/image.png)
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 件のコメント
採用された回答
Guillaume
2020 年 2 月 26 日
編集済み: Guillaume
2020 年 2 月 26 日
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)));
2 件のコメント
Image Analyst
2020 年 2 月 26 日
If you need more help, attach temp in a .mat file using the paper clip icon, so we can work with it
save('answers.mat', 'temp'); % Save temp to a file on disk.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!