Effacer les filtres
Effacer les filtres

How to add values to structure array without loop

3 vues (au cours des 30 derniers jours)
Durga Lal Shrestha
Durga Lal Shrestha le 8 Déc 2015
Consider the following loop:
values = [2 5 6 4 8 9 10 5 15 7]
for i=1:10
arr(i).myfield = values(i);
end
How can this be done without loop?
Thank you.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Déc 2015
T = struct('myfield', num2cell(values));
arr(1:length(T)) = T;
In the special case that arr does not already exist, it can be done with the one line
arr = struct('myfield', num2cell(values));
  4 commentaires
Durga Lal Shrestha
Durga Lal Shrestha le 8 Déc 2015
Thanks. It works now.
Somaye Hamedi Bazaz
Somaye Hamedi Bazaz le 24 Nov 2018
Great!!!!!!!!!!!! thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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