Clear all fields of a struct
97 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a struct with a number of fields that I want populate, and then store, each iteration through a loop. I want the struct to be empty and available each iteration. Is there a function to clear all the fields? Or do I just declare the whole thing again?
I tried setting each feild individually to [], like
myStruct.a = [];
myStruct.b = [];
But it's actually faster to declare the whole thing new each iteration, as in:
myStruct = struct('a', [], 'b', []);
Or maybe there's a better approach all together?
Cheers, Karl
0 个评论
采纳的回答
Karl
2014-3-26
1 个评论
Azzi Abdelmalek
2014-3-26
clearing your variable doesn't make your code faster, maybe you need to pre-allocate
myStruct = structfun(@(x) zeros(1,100), myStruct, 'UniformOutput', false);
更多回答(1 个)
Azzi Abdelmalek
2014-3-26
编辑:Azzi Abdelmalek
2014-3-26
%example
a.b=1
a.c=2
a.d=3
%----------------
f=fields(a)
for k=1:numel(f)
a.(f{k})=[];
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!