Effacer les filtres
Effacer les filtres

Assign a 'double' variable to struct

12 vues (au cours des 30 derniers jours)
Ziv Kassner
Ziv Kassner le 10 Jan 2018
Commenté : Ziv Kassner le 11 Jan 2018
I have a variable as such:
A.b = 'example';
And I want to change it to:
A.b.c = 2;
How can I do it without it prompting:
'Field assignment to a non-structure array object'
It worked on matlab's version before 2014.
Thank you,
Ziv
  2 commentaires
Stephen23
Stephen23 le 10 Jan 2018
You can't.
You first define the field b as a double. You cannot then try to access field b as a structure.
Steven Lord
Steven Lord le 10 Jan 2018
FYI this changed in release R2015b according to the Release Notes.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 10 Jan 2018
Modifié(e) : Jan le 10 Jan 2018
A.b = 'example';
A.b = struct('c', 2); % Overwrite field "b"
Or:
A.b = 'example';
A.b = [];
A.b.c = 2
It is strange, that this works with the empty matrix, if it fails with an error for a char vector. But the first method is better, because it overwrites the field clearly.
  1 commentaire
Ziv Kassner
Ziv Kassner le 11 Jan 2018
Great! The 1st method made it happen!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Axes Appearance 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