How to split a column with numerical values?
3 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I have a 1x72 structure with one column containing three numerical values (attched).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/911585/image.png)
I would like to convert it into a 3x72 table with each numerical value in a seperate column with diffrent name, for example:
'name 1' 'name 2' 'name 3'
0.9377 0.9246 0.9982
Can you help please?
0 件のコメント
採用された回答
Stephen23
2022 年 3 月 2 日
編集済み: Stephen23
2022 年 3 月 2 日
Here are two robust MATLAB approaches (both much more robust than blindly using STRUCT2ARRAY):
S = load('data.mat');
ICCs = S.ICCs
T = array2table(vertcat(ICCs.Metrics),'VariableNames',{'XX','YY','ZZ'})
T = splitvars(struct2table(ICCs),'Metrics','NewVariableNames',{'XX','YY','ZZ'})
その他の回答 (2 件)
KSSV
2022 年 3 月 2 日
編集済み: KSSV
2022 年 3 月 2 日
load('data.mat')
name = reshape(struct2array(ICCs),3,[])' ;
T = array2table(name);
Or you can use
load('data.mat')
T = struct2table(ICCs);
1 件のコメント
Stephen23
2022 年 3 月 2 日
編集済み: Stephen23
2022 年 3 月 2 日
Avoid this code, it is not robust. Consider what would happen if the structure contains other fields.
One of the main benefits of using structures is that each field can be processed independently. This approach using STRUCT2ARRAY discards that benefit, making the code more fragile to future data changes.
EDIT: after KSSV editted and added a second approach: the second code suggestion does not work because the OP requested three columns in the table, and that code only returns one.
Sigh.
Mathieu NOE
2022 年 3 月 2 日
hello
this would be my suggestion
load data.mat
% convert structure to numerical array
a = struct2cell(ICCs);
a = squeeze(a);
a = vertcat(a{:});
% create table
t = array2table(a,'VariableNames',{'name 1' 'name 2' 'name 3'});
1 件のコメント
Stephen23
2022 年 3 月 2 日
One of the main benefits of using structures is that each field can be processed independently. This approach using STRUCT2CELL discards that benefit, making the code more fragile to future data changes.
SQUEEZE (which is an abomination anyway) is not required if the variable is used for a comma-separated list.
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!