Concatenating columns while reading tables

9 vues (au cours des 30 derniers jours)
Luis FigueroaFernandez
Luis FigueroaFernandez le 10 Mai 2021
Commenté : Walter Roberson le 10 Mai 2021
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

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Mai 2021
Modifié(e) : Walter Roberson le 10 Mai 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 commentaires
Luis FigueroaFernandez
Luis FigueroaFernandez le 10 Mai 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 le 10 Mai 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by