Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
struct array to cell array of <1x1 struct>

Subject: struct array to cell array of <1x1 struct>

From: icklePhil

Date: 16 Nov, 2010 12:17:05

Message: 1 of 3

Hello everyone

I'm trying to create a cell array out of a struct array.
I know that there is the MATLAB function struct2cell, but it does not quite do what I want:
If I have a struct array like

a = struct('f1', {'one','two','three'}, 'f2', {1,2,3});

and then use struct2cell on that

b = struct2cell(a);

I get a cell array of size 2x3 that looks like

b =
        'one' 'two' 'three'
         [1] [2] [3]

But what I'm trying to achieve is a cell array of size 1x3, containing structs, like

c =
        [1x1 struct] [1x1 struct] [1x1 struct]

where each 1x1 struct is one of the entries of a, still in struct form.
Is there a better solution for this besides using a for loop like this?

c = cell(size(a));
for i=1:size(a,2)
    c{i} = a(i);
end

Cheers, icklePhil

Subject: struct array to cell array of <1x1 struct>

From: Bruno Luong

Date: 16 Nov, 2010 13:45:07

Message: 2 of 3

c = num2cell(a)

% Bruno

Subject: struct array to cell array of <1x1 struct>

From: icklePhil

Date: 16 Nov, 2010 14:23:03

Message: 3 of 3

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibu1t3$flh$1@fred.mathworks.com>...
> c = num2cell(a)
>
> % Bruno

hi Bruno

it works like a charm, thank you very much!
I had no idea it would be so simple. When looking for a solution I unfortunately did not think of finding a solution among the numerical array handling functions...

cheers, icklePhil

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us