Extracting data from cell array

Let's say that i have
data =
1×4 cell array
{12×1 cell} {12×1 cell} {12×1 int32} {12×1 int32}
first 12x1 cell array contains Names, second sournames, third age and forth id numer.
I would like to do something like extract just 1st elements from each cell array - it woul'd give me an array of data for one person, then do it for the next 11 rows.
How to make it? It should be convert into struct, and I know that repmat function can be useful here, but i have no clue how to do it.

1 件のコメント

KSSV
KSSV 2020 年 11 月 29 日
You can convert cell to structure or table using cell2struct and cell2table. Read about them and then it should be easy to follow.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2020 年 11 月 29 日
編集済み: Image Analyst 2020 年 11 月 29 日

0 投票

It would help you to learn about tables, which are more appropriate in this case than cell arrays:
% Original cell array:
data = {{'Smith' ; 'Gold'}, {'Ann' ; "Agnes"}, {'22' ; '24'}, {'1' ; '2'}}
% Convert cell array into table:
t = table(data{1}, data{2}, data{3}, data{4}, ...
'VariableNames', {'LastName', 'FirstName', 'Age', 'IDNumber'})
% Get out row 1:
a = t(1, :)
% Get out row 2:
b = t(2, :)

その他の回答 (1 件)

KSSV
KSSV 2020 年 11 月 29 日

1 投票

T = cell2table(C,...
'VariableNames',{'Name' 'SirName' 'Age' 'Id'}) ;
T
You have the data in Table T, you can extract what you want.

4 件のコメント

M
M 2020 年 11 月 29 日
But how to extract for example just 3rd elements from {'Name' 'SirName' 'Age' 'Id'}, and save it as one array?
KSSV
KSSV 2020 年 11 月 29 日
編集済み: KSSV 2020 年 11 月 29 日
data = table2array(T)
You can also try using cell2mat striaghtaway with C.
M
M 2020 年 11 月 29 日
cell2mat also would give me just array of everything. I'll try to describe it more clearly.
data = {{'Smith' ; 'Gold' ; }, {'Ann' ; "Agnes"}, {'22' ; '24'}, {'1' ; '2'}};
So i want to have an array from it which would output for example:
a = ['Smith' ; 'Ann' ; '22' ; '1'];
b = ['Gold' ; 'Agnes' ; '24' ; '2']; %and so one(cause i have more data like this)
KSSV
KSSV 2020 年 11 月 29 日
Table is apt for you.....you have it in T.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2020b

質問済み:

M
M
2020 年 11 月 29 日

コメント済み:

2020 年 11 月 29 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by