Hojo.Timberwolf
Hojo.Timberwolf

Reputation: 1091

multi structure output to list

Below is some code that will work in extracting out the a cell array of ID names from the structure. It uses a for loop to achieve this. However I am wondering if there is a way to perform the same task without using a forloop?

tft(1).Id = 'Name1';
tft(1).Desc = 'goes by the name';
tft(2).Id = 'Name2';
tft(2).Desc = 'hates the name';

for a=1:length(tft)
    list{a} = tft(a).Id
end

Upvotes: 1

Views: 18

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114300

There is a documentation page dedicated to this question: Access Elements of a Nonscalar Struct Array.

Since doing tft.Id returns a comma separated list, you can convert it into a cell array directly by enclosing it in curly braces:

list = {tft.Id};

Upvotes: 2

Related Questions