How to convert an array of cells into an array of structures?
1 次查看(过去 30 天)
显示 更早的评论
Hello, I want convert an array of cells into a an array of structures:
address1 = { 'aaa'; 'bbb'; 'ccc' }
address2 = { 'ddd'; 'eee'; 'fff' }
address3 = { 'ggg'; 'hhh'; 'iii' }
address4 = { 'jjj'; 'kkk'; 'lll' }
address5 = { 'mmm'; 'nnn'; 'ooo' }
addresses = {address1; address2; address3; address4; address5}
colHeadings = { 'name'; 'address'; 'city'}
addressStruct = cell2struct(addresses, colHeadings, 2)
But this does not work, I got the following error:
addresses =
5×1 cell array
{3×1 cell}
{3×1 cell}
{3×1 cell}
{3×1 cell}
{3×1 cell}
colHeadings =
3×1 cell array
{'name' }
{'adress'}
{'city' }
Error using cell2struct
Number of field names must match number of fields in new structure.
Error in e4 (line 11)
addressStruct = cell2struct(adresses, colHeadings, 1)
May be the problem is, that a single address is a vertikal array 3*1. How can I do this in right way?
Thank you very much for help!
0 个评论
采纳的回答
Prateek Rai
2020-6-19
As per my understanding you are trying to convert an array of cells into an array of structures but "cell2struct" is throwing an error. I think the Issue is with the way of concatenating all addresses in “addresses”. Use “vertcat” instead to concatenate the cell arrays.
Here is the sample code :
address1 = { 'aaa' 'bbb' 'ccc' }
address2 = { 'ddd' 'eee' 'fff' }
address3 = { 'ggg' 'hhh' 'iii' }
address4 = { 'jjj' 'kkk' 'lll' }
address5 = { 'mmm' 'nnn' 'ooo' }
addresses = vertcat(address1, address2, address3, address4, address5)
colHeadings = { 'name'; 'address'; 'city'}
addressStruct = cell2struct(addresses, colHeadings, 2)
You can refer to the following documentation for more information on “vertcat”.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!