Deleting fields of a particular dimension in a structure

1 visualizzazione (ultimi 30 giorni)
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.

Risposte (1)

Stephen23
Stephen23 il 25 Gen 2023
Modificato: Stephen23 il 25 Gen 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))
S = struct with fields:
A: [0.5803 0.2030 0.0122 0.9301 0.2433 0.3086 0.5901 0.1738 0.2942 0.0230 0.2622 0.0181 0.6488 0.0865 0.4528 0.4397 0.0823 0.7363 0.9030 0.4084 0.7442 0.4275 0.0978 0.9522 0.1738 0.4499 0.6503 0.8244 0.2360 0.6347 0.3943 0.5161 0.6434 0.0138 … ] B: [0.4005 0.7590 0.7921 0.4481 0.9009 0.8044 0.1968 0.7716 0.7309 0.0090 0.3974 0.1231 0.9473] C: [0.2528 0.5823 0.9105 0.8959 0.0374 0.8646 0.0330 0.5280 0.3386 0.3714 0.1557 0.2200 0.6784 0.7183 0.4370 0.4224 0.4348 0.1134 0.5370 0.8962 0.8624 0.1554 0.7767 0.7557 0.4659 0.2342 0.5080 0.4323 0.5891 0.3560 0.6953 0.8060 0.4946 0.2856 … ] D: [0.7431 0.2480 0.7271 0.0565 0.2234 0.4784 0.9350 0.3833 0.0551 0.1141 0.2919 0.9120 0.3175] E: [0.1765 0.3280 0.1030 0.4814 0.7136 0.0351 0.0152 0.6603 0.1930 0.6685 0.5606 0.6725 0.1345 0.1871 0.0278 0.1757 0.0406 0.1327 0.3057 0.6876 0.0991 0.7010 0.1206]
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)
Z = struct with fields:
A: [0.5803 0.2030 0.0122 0.9301 0.2433 0.3086 0.5901 0.1738 0.2942 0.0230 0.2622 0.0181 0.6488 0.0865 0.4528 0.4397 0.0823 0.7363 0.9030 0.4084 0.7442 0.4275 0.0978 0.9522 0.1738 0.4499 0.6503 0.8244 0.2360 0.6347 0.3943 0.5161 0.6434 0.0138 … ] C: [0.2528 0.5823 0.9105 0.8959 0.0374 0.8646 0.0330 0.5280 0.3386 0.3714 0.1557 0.2200 0.6784 0.7183 0.4370 0.4224 0.4348 0.1134 0.5370 0.8962 0.8624 0.1554 0.7767 0.7557 0.4659 0.2342 0.5080 0.4323 0.5891 0.3560 0.6953 0.8060 0.4946 0.2856 … ] E: [0.1765 0.3280 0.1030 0.4814 0.7136 0.0351 0.0152 0.6603 0.1930 0.6685 0.5606 0.6725 0.1345 0.1871 0.0278 0.1757 0.0406 0.1327 0.3057 0.6876 0.0991 0.7010 0.1206]
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)
U = 3×1
13 23 12800
N = histcounts(V,[U;Inf])
N = 1×3
2 1 2
  3 Commenti
Anwesh Saha
Anwesh Saha il 26 Gen 2023
How do I delete say the fields with dimensions 1X13?
Stephen23
Stephen23 il 26 Gen 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.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by