필터 지우기
필터 지우기

How to split a column with numerical values?

조회 수: 4 (최근 30일)
Tomaszzz
Tomaszzz 2022년 3월 2일
댓글: Stephen23 2022년 4월 26일
Hi all,
I have a 1x72 structure with one column containing three numerical values (attched).
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?

채택된 답변

Stephen23
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
ICCs = 1×72 struct array with fields:
Metrics
T = array2table(vertcat(ICCs.Metrics),'VariableNames',{'XX','YY','ZZ'})
T = 72×3 table
XX YY ZZ _______ _______ _______ 0.93769 0.92456 0.99817 0.86066 0.90427 0.99263 0.84509 0.85719 0.98951 0.8738 0.89004 0.99273 0.55918 0.68187 0.55212 0.7432 0.56476 0.70838 0.67489 0.51727 0.64014 0.764 0.72324 0.6591 0.81925 0.90581 0.90397 0.71105 0.74472 0.83758 0.71823 0.71584 0.84393 0.90053 0.85199 0.96139 0.90284 0.82024 0.98977 0.78653 0.76117 0.97559 0.75978 0.70471 0.97234 0.78341 0.70018 0.97691
T = splitvars(struct2table(ICCs),'Metrics','NewVariableNames',{'XX','YY','ZZ'})
T = 72×3 table
XX YY ZZ _______ _______ _______ 0.93769 0.92456 0.99817 0.86066 0.90427 0.99263 0.84509 0.85719 0.98951 0.8738 0.89004 0.99273 0.55918 0.68187 0.55212 0.7432 0.56476 0.70838 0.67489 0.51727 0.64014 0.764 0.72324 0.6591 0.81925 0.90581 0.90397 0.71105 0.74472 0.83758 0.71823 0.71584 0.84393 0.90053 0.85199 0.96139 0.90284 0.82024 0.98977 0.78653 0.76117 0.97559 0.75978 0.70471 0.97234 0.78341 0.70018 0.97691

추가 답변 (2개)

KSSV
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
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
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
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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by