Concatenating columns while reading tables

조회 수: 8 (최근 30일)
Luis FigueroaFernandez
Luis FigueroaFernandez 2021년 5월 10일
댓글: Walter Roberson 2021년 5월 10일
I am trying to concatenate columns into arrays of (n:3) while reading an ASCII file as a table:
t= radtable(filename, 'ReadRowNames', true, 'VariableNamesLine', 1)
My data looks like folowing:
ITEM X Y Z X Y Z
1 -6.1682 -9.7558 -27.7597 93.5565 32.3906 -45.2551
2 -6.1654 -9.7477 -27.7617 93.5520 32.3927 -45.2465
3 -6.1765 -9.7458 -27.7552 93.5581 32.3897 -45.2449
Currently Matlab reads the file as each column as it's own, but I need to treat each xyz as an array for easier management and I want to avoid using loops to rearrange data as I have >700 columns in each file.
The result I wan to get to looks like:
ITEM 1 2
____ __________________ __________________
1 (-6.1682, -9.7558, -27.7597) (93.5565, 32.3906, -45.2551)
2 (-6.1682, -9.7558, -27.7597) (93.5565, 32.3906, -45.2551)
3 (-6.1682, -9.7558, -27.7597) (93.5565, 32.3906, -45.2551)
Thank you
Al

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 10일
편집: Walter Roberson 2021년 5월 10일
ngroup = (size(t,2)-1)/3;
newt = [t(:,1),cell2table(mat2cell(t{:,2:end}, ones(1,size(t,1)), 3 * ones(1,ngroup)))];
newt.Properties.VariableNames(2:end) = cellstr(string(1:ngroup));
  댓글 수: 2
Luis FigueroaFernandez
Luis FigueroaFernandez 2021년 5월 10일
Walter Roberson, you are magician, thank you so much for your help.
The only change I had to do was to rename the first variable so I wouldn't get a duplicate of the variable name:
t.Properties.VariableNames{1} = 'frame';
Walter Roberson
Walter Roberson 2021년 5월 10일
You read the variable names into t, so the first column should already be named Item and the code would not duplicate that name.
You would only get the error you are referring to if you did not read in the variable names like your code shows you doing... or the first variable name just happened to be 'Var' followed by a digit.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by