Merge Variables in Table

1 vue (au cours des 30 derniers jours)
Alexandra Kopaleyshvili
Alexandra Kopaleyshvili le 12 Juin 2021
I would like to create a for loop which can merge variables dynamically by first letter, excluding the number standing behind the letter.
from this:
to this:
Thanks!

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Juin 2021
names = YourTable.Properties.VariableNames;
initials = extractBefore(names, 2);
[G, id] = findgroups(initials);
newTable = table();
for K = 1 : length(id)
newTable.(id{K}) = [YourTable{:,G==K}];
end
  2 commentaires
Alexandra Kopaleyshvili
Alexandra Kopaleyshvili le 13 Juin 2021
Hey Walter, thank you very much!
Is it possible to combine those multicolumn variables (like B or L) into single-column-variables (as shown in the second picture)?
Alexandra Kopaleyshvili
Alexandra Kopaleyshvili le 13 Juin 2021
Modifié(e) : Alexandra Kopaleyshvili le 13 Juin 2021
Ok i got it! I just needed to add this to your code:
for jj=1:width(newTable)
size(newTable.(jj))
if ans(1,2)> 1
alinear = {newTable.(jj){:}}'
{cat(1, alinear{:})}
newTable.(jj)=ans
end
end

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