필터 지우기
필터 지우기

Delete fields of struct based on starting characters

조회 수: 3 (최근 30일)
Timon Rayis
Timon Rayis 2020년 2월 26일
댓글: Timon Rayis 2020년 2월 26일
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.

채택된 답변

Guillaume
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
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.
Timon Rayis
Timon Rayis 2020년 2월 26일
Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by