Deleting fields of a particular dimension in a structure
1 view (last 30 days)
Show older comments
I have a struct with ints in it. I want to delete the fields with 1X13 int32 in it. Also, I would like to know the count of different fields with respect to its dimension. For example:- 120 fields with dimension int 1X12800.
0 Comments
Answers (1)
Stephen23
on 25 Jan 2023
Edited: Stephen23
on 25 Jan 2023
As I wrote in several of your earlier questions, this task would be much easier if your data were better designed:
But lets try your preferred complex aproach. First some fake data:
S = struct('A',rand(1,12800),'B',rand(1,13),'C',rand(1,12800),'D',rand(1,13),'E',rand(1,23))
C = struct2cell(S);
F = @(a)isequal(size(a),[1,13]);
X = cellfun(F,C,'uni',1);
B = fieldnames(S);
Z = cell2struct(C(~X),B(~X),1)
This would be much easier using some kind of indexed array class, rather than forcing pseudo-indices into fieldnames.
"For example:- 120 fields with dimension int 1X12800."
V = cellfun(@(a)size(a,2),C);
U = unique(V)
N = histcounts(V,[U;Inf])
3 Comments
Stephen23
on 26 Jan 2023
"When I run the command given in the previous answers by you, I run into the following error:"
I cannot debug code that you do not show here.
Providing the complete error message is also a requirement, if you want further help.
"How do I delete say the fields with dimensions 1X13?"
That is what my answer does. Take a look at my answer.
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!