Effacer les filtres
Effacer les filtres

question about the 'stuct' of matlab?

2 vues (au cours des 30 derniers jours)
Ray Lee
Ray Lee le 24 Nov 2014
Commenté : Ray Lee le 26 Nov 2014
Why the second one gives rise to empty struct?
s.a=1; s.b=2; s.c={}
s =
a: 1
b: 2
c: {}
s = struct('a',1,'b',2,'c',{})
s =
0x0 struct array with fields:
a
b
c

Réponse acceptée

per isakson
per isakson le 24 Nov 2014
Modifié(e) : per isakson le 24 Nov 2014
That's the way the function struct is designed to work. See struct, Create structure array "[...]If value is an empty cell array {}, then s is an empty (0-by-0) structure."
  4 commentaires
Guillaume
Guillaume le 24 Nov 2014
Modifié(e) : Guillaume le 24 Nov 2014
If you do want to assign an empty cell array to a field of a structure using the struct function, you need to wrap that empty cell array into a cell array:
s = struct('a', 1, 'b', 2, 'c', {{}})
s =
a: 1
b: 2
c: {}
For that matter, even if the cell array is not empty you still need to wrap it to assign it to a scalar structure, otherwise the elements of the cell arrays are just distributed over an array of structs.
Ray Lee
Ray Lee le 26 Nov 2014
good solution.
in my code, I checked if s.c is empty. if not, the code makes some processing using the parameters in s.c

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by