Removing all elements from a struct field

2 views (last 30 days)
Tamer Gezici
Tamer Gezici on 14 Jun 2022
Commented: Paul on 14 Jun 2022
I have a struct
>> aap.acq_details.sessions
ans =
1×13 struct array with fields:
name
I want to simply make this "name" field empty, ie. remove/reset all elements in this specific field.
  2 Comments
Stephen23
Stephen23 on 14 Jun 2022
"So I need it completely deleted, blank. It should not even have empty array in it."
MATLAB does not have a "blank". The default for unallocated cells and fields is an empty array.
C{2} = pi;
C{1}
ans = []
S(2).F = pi;
S(1).F
ans = []

Sign in to comment.

Accepted Answer

Paul
Paul on 14 Jun 2022
Does rmfield meet the need?
% create a struct for example
for ii=1:13
aap.acq_details.sessions(ii).name = ii;
end
aap.acq_details.sessions
ans = 1×13 struct array with fields:
name
aap.acq_details.sessions = rmfield(aap.acq_details.sessions,'name');
aap.acq_details.sessions
ans = 1×13 struct array with no fields.
  2 Comments
Paul
Paul on 14 Jun 2022
Then do what you did in the comment to the question. Once name is empty, you can do whatever you want with name.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 14 Jun 2022
[aap.acq_details.sessions.name]=deal([])

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by