Clear all fields of a struct
Show older comments
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
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 26 Mar 2014
Edited: Azzi Abdelmalek
on 26 Mar 2014
%example
a.b=1
a.c=2
a.d=3
%----------------
f=fields(a)
for k=1:numel(f)
a.(f{k})=[];
end
1 Comment
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!