Concatenating columns while reading tables

8 次查看(过去 30 天)
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
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 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by