Concatenating columns while reading tables

6 ビュー (過去 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 Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by