Effacer les filtres
Effacer les filtres

Changing field value for all fields in a struct

30 vues (au cours des 30 derniers jours)
MiauMiau
MiauMiau le 3 Avr 2014
Commenté : deejt le 19 Jan 2021
Hi
Given for instance I have a struct as follows:
s(2) = struct('name','anna','age',18)
s(1) = struct('name','bernd','age',22)
Now I want to change all the names to 'unknown', i.e. as well for s(1) as for s(2). How can I do it (this is just a toy example, I do have a 123 dimensional struct where I do not want to do it one by one). Thanks

Réponse acceptée

Friedrich
Friedrich le 3 Avr 2014
Hi,
without a loop:
[s.name] = deal('unknown')
  2 commentaires
MiauMiau
MiauMiau le 3 Avr 2014
wow thanks
deejt
deejt le 19 Jan 2021
How would you proceed if you would only change the name 'anna' to unknown but leave 'bernd'?

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 3 Avr 2014
s(2) = struct('name','anna','age',18)
s(1) = struct('name','bernd','age',22)
n=numel(s)
for k=1:n
s(k).name='unknown'
end

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by