Removing all elements from a struct field

2 ビュー (過去 30 日間)
Tamer Gezici
Tamer Gezici 2022 年 6 月 14 日
コメント済み: Paul 2022 年 6 月 14 日
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 件のコメント
Tamer Gezici
Tamer Gezici 2022 年 6 月 14 日
編集済み: Tamer Gezici 2022 年 6 月 14 日
Someone answered (but now that's deleted for some reason) by saying I should do it like this:
for i=1:numel(aap.acq_details.sessions)
aap.acq_details.sessions(i).name =[];
end
However, this doesn't remove the field. It just sets it to empty array. I want to empty this struct, and put my own values. So I need it completely deleted, blank. It should not even have empty array in it.
Stephen23
Stephen23 2022 年 6 月 14 日
"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 = []

サインインしてコメントする。

採用された回答

Paul
Paul 2022 年 6 月 14 日
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 件のコメント
Tamer Gezici
Tamer Gezici 2022 年 6 月 14 日
編集済み: Tamer Gezici 2022 年 6 月 14 日
Yes, kind of. I already used rmfield but now the issue is, I need to create that field called "name" again. So I don't want to completely remove the field, but just clean it, purge it whaatever. And it needs to be empty so I can add new values on my own to the end of that array (append them). How can I do that?
Paul
Paul 2022 年 6 月 14 日
Then do what you did in the comment to the question. Once name is empty, you can do whatever you want with name.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2022 年 6 月 14 日
[aap.acq_details.sessions.name]=deal([])

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by