Concatenating columns while reading tables

7 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 2021
Edited: Walter Roberson on 10 May 2021
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 Comments
Luis FigueroaFernandez
Luis FigueroaFernandez on 10 May 2021
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 on 10 May 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!