matlab

MATLAB: How can I convert a cell array of structs into a struct array using the colon operator?


Assume there is a cell array initialized with the following struct values.

% Phone book
phone_record{1} = struct('name', 'Bob', 'phone', '1233323');
phone_record{2} = struct('name', 'Mike', 'phone', '3245524');

% How can I make such a or similar one-liner work?
% phonebook(:) = phone_record{:}

% Expected:
% phonebook(1).name = 'Bob';
% phonebook(1).phone= '1233323';
% phonebook(2).name = 'Mike';
% phonebook(2).phone = '3245524';

Is it indeed possible to accomplish this without using cell2struct or for loop indexing? Can one use deal or similar?


Solution

  • You can use cell2mat :

    cell2mat(phone_record)
    

    ans =

    1x2 struct array with fields:

    name
    phone