How do I delete a field variable from a structure

조회 수: 30 (최근 30일)
Martin
Martin 2012년 2월 19일
댓글: Trung Hieu Le 2016년 6월 6일
I have variable in gui: handles.Data; Data is type: 70x50x2 logical. I need to delete the variable data. When i write: delete(handles.Data); or clean(handles.Data); then get an error: Argument must contain a string. :(
Thank you
  댓글 수: 1
Jiro Doke
Jiro Doke 2012년 2월 19일
I'm going to rename the title to better reflect the question.

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

채택된 답변

Jiro Doke
Jiro Doke 2012년 2월 19일
handles.Data is a field of variable handles. To delete a field,
handles = rmfield(handles. 'Data');
To set the field to empty,
handles.Data = [];
  댓글 수: 1
Martin
Martin 2012년 2월 19일
Finally, it works as follows: handles=rmfield(handles,'Data');
Thx!

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

추가 답변 (2개)

Jan
Jan 2012년 2월 19일
Same effect as rmfield, but about 10 times faster: FEX: fRMField

Image Analyst
Image Analyst 2012년 2월 19일
Try this robust code, to avoid errors:
% Try to remove the field called Data, if there is one.
% First see if there is a field called "Data."
hasField = isfield(handles, 'Data') % Will be True or False.
% Now remove it if it's there.
if hasField
% Field is there. Remove it.
handles = rmfield(handles, 'Data')
else
% Field is not there, warn user.
warningMessage = sprintf('Warning: the structure "handles"\ndoes not have a field called "Data."');
uiwait(warndlg(warningMessage));
end
  댓글 수: 2
Martin
Martin 2012년 2월 20일
great, it'll use
thx
Trung Hieu Le
Trung Hieu Le 2016년 6월 6일
Thanks for this answer. It is perfect.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by