Azzera filtri
Azzera filtri

Create tables based on matching columns by identical names

3 visualizzazioni (ultimi 30 giorni)
I have two tables with four separate columns. Each column in each table has a unique variable name. However, the variable names match between position 5 and 10 for two columns (one in each table). I want to create four new tables consisting of two columns where the variable names match. So far, I have tried the following:
names5 = Table_A.Properties.VariableNames;%1
names6 = Table_B.Properties.VariableNames;%1
matchVals = {'1234', '5678','9101','1123'}; %part of the variable names that match => four new tables
numTables = numel(matchVals);
tableNames_ = cell(numTables,1);
for k = 1:numel(matchVals)
idl5 = cellfun(@(x) strcmp(x(5),matchVals{k})... %assessing position 5 to 10 for correct variable names
idl6 = cellfun(@(x) strcmp(x(5),matchVals{k})...%assessing position 5 to 10 for correct variable names
eval(['Summary',matchVals{k},' = [Table_A(:,idl5) Table_B(:,idl6)]']);
tableNames_EC2217{k} = ['Summary',matchVals{k}]; %trying to create new table
The code runs, but my columns are not matched. I.e. my out put includes the new tables but without any row values from the original tables.
Any help is appreciated.

Risposta accettata

Vatsal il 26 Set 2023
Hi @Vlatko,
I understand that you have two tables “Table_A” and “Table_B” and both the tables contain four columns, each column having a unique variable name. Given that the variable names in the two columns (one from each table) match between positions 5 and 10, the objective is to generate four new tables. These tables will specifically contain two columns each, where the variable names match.
I am attaching the code which creates four new tables consisting of two columns where the variable names match: -
names5 = Table_A.Properties.VariableNames;
names6 = Table_B.Properties.VariableNames;
matchVals = {'1234', '5678', '9101', '1123'};
numTables = numel(matchVals);
tableNames_ = cell(numTables, 1);
for k = 1:numel(matchVals)
pattern = matchVals{k};
startIdx = 5; % Starting index
endIdx = 10; % Ending index
idl5 = cellfun(@(x) contains(x(startIdx:endIdx), pattern), names5);
idl6 = cellfun(@(x) contains(x(startIdx:endIdx), pattern), names6);
eval(['Summary', matchVals{k}, ' = [Table_A(:, idl5) Table_B(:, idl6)];']);
tableNames_{k} = ['Summary', matchVals{k}];
You can also refer to the MATLAB documentation for the function "contains" to obtain more information on its usage and syntax. The link is provided below:
I hope this helps!

Più risposte (0)


Scopri di più su Structures in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by