How to concatenate multiple Tables by comparing it to the variables associated with individual tables in workspace.

26 visualizzazioni (ultimi 30 giorni)
Hello Everyone,
I would try my best to explain the situation in the best possible way.
1) In the below code, the variables of interest or the ones I need suggestion with are "Pattern_List_Transposed".
when the loop begins it checks conditions with IF whichever conditon comes true & I finally concatinate a table below under Summary_Table.
This Would not work if any of the cases under the IF & elseif aren't satisfied then this table will not be Concatinated.
Example: I do have files where for instance _00 & _02 related data may exist and other cases _00,_01,_05,_04, or any other combination. I want the code to work even if the file has variables with just one extension or multiple extensions.
If we can create a summary table based on what small tables are spit out in Workspace within the Condition would be ideal and I am unable to do what will work.
Attaching a few sample files.
clear;
clc;
[filelist,pathn] = uigetfile('*.txt','Please Select a Par Files', 'multiselect', 'on');
if isnumeric(filelist); error('no file selected');
end
filelist = cellstr(filelist);
filelist = fullfile(pathn, filelist);
N = numel(filelist);
% Output_Directory = 'D:\Odera\Results_2';
% In line 7 Paste your path where output excel files will be written to.
% Make sure that folder/Drive is added to Matlab path.
% Also Make sure the Folder the .txt files are saved in do not have
% "_NGHD"as part for their folder Name.
Output_Filename = extractBetween(filelist,"NGHD_",".txt") + ".xlsx";
% Output_File_Names = fullfile(Output_Directory,Output_Filename);
tic
for K = 1:N
filename = filelist{K};
file_Details = importdata(filename);
Data_in = readtable(filename, 'Delimiter',{',',';',' '});
is_empty = strcmp(Data_in.Var1,':').';
s_idx = strfind([false is_empty],[false true]);
e_idx = strfind([is_empty false],[true false]);
for ii = 1:numel(s_idx)
Data_in{s_idx(ii):e_idx(ii),1} = Data_in{s_idx(ii)-1,1};
end
All_Para = Data_in.Var1;
Slot_PG_List = {'C_PG_SlotPGID_00';'C_PG_SlotPGID_01';'C_PG_SlotPGID_02';'C_PG_SlotPGID_03';'C_PG_SlotPGID_04';'C_PG_SlotPGID_05';'C_PG_SlotPGID_06'};
Compare_PG_List = extractAfter(Slot_PG_List,"SlotPGID_");
Compare_Slots = contains(All_Para,Slot_PG_List);
Slot_Data = Data_in(Compare_Slots,:);
Slot_Data_Values = lt(Slot_Data.Var4,255);
Slots_Active = Slot_Data.Var1(Slot_Data_Values);
Pattern_For_List = extractAfter(Slots_Active,"SlotPGID");
Extracting_List = find(endsWith(Data_in.Var1,Pattern_For_List));
Extracting_List_Data = Data_in(Extracting_List,["Var1","Var3","Var4","Var7","Var8"]);
Pattern_List_Transposed = transpose(Pattern_For_List);
for J =1:length(Pattern_List_Transposed)
if Pattern_List_Transposed{J} == "_02"
Per_Match = find(endsWith(Extracting_List_Data.Var1,"_02"));
Per_Table = Extracting_List_Data(Per_Match,["Var1","Var3","Var4","Var7","Var8"]);
elseif Pattern_List_Transposed{J} == "_03"
Tanker_Match = find(endsWith(Extracting_List_Data.Var1,"_03"));
Tanker_Table = Extracting_List_Data(Tanker_Match,["Var1","Var3","Var4","Var7","Var8"]);
elseif Pattern_List_Transposed{J} == "_04"
Off_Match = find(endsWith(Extracting_List_Data.Var1,"_04"));
Off_Table = Extracting_List_Data(Off_Match,["Var1","Var3","Var4","Var7","Var8"]);
elseif Pattern_List_Transposed{J} == "_00"
Eco_Match = find(endsWith(Extracting_List_Data.Var1,"_00"));
Eco_Table = Extracting_List_Data(Eco_Match,["Var1","Var3","Var4","Var7","Var8"]);
elseif Pattern_List_Transposed{J} == "_01"
Standard_Match = find(endsWith(Extracting_List_Data.Var1,"_01"));
Standard_Table = Extracting_List_Data(Standard_Match,["Var1","Var3","Var4","Var7","Var8"]);
elseif Pattern_List_Transposed{J} == "_05"
Down_Match = find(endsWith(Extracting_List_Data.Var1,"_05"));
Down_Table = Extracting_List_Data(Down_Match,["Var1","Var3","Var4","Var7","Var8"]);
elseif Pattern_List_Transposed{J} == "_06"
Heavy_Match = find(endsWith(Extracting_List_Data.Var1,"_06"));
Heavy_Table = Extracting_List_Data(Heavy_Match,["Var1","Var3","Var4","Var7","Var8"]);
end
Sumary_Table = [Eco_Table,Per_Table,Standard_Table,Tanker_Table,Off_Road,Down_Table,Heavy_Table];
end
end

Risposta accettata

Voss
Voss il 27 Mar 2024
Modificato: Voss il 27 Mar 2024
"This Would not work if any of the cases under the IF & elseif aren't satisfied then this table will not be Concatinated. "
That's true, and in fact at most one if or elseif condition will be satisfied in each iteration of the inner (J) for loop, which means that at most one of Eco_Table, Per_Table, Standard_Table, Tanker_Table, Off_Table, Down_Table, Heavy_Table will be defined on the first loop iteration, so that loop will always fail with an error on the Sumary_Table line on its first iteration because most if not all of the variables it uses are undefined. Also, you're attempting horizontal concatenation, which won't work due to the same table variable names existing in each table. (And even if that concatenation worked, Sumary_Table is overwritten on each iteration.)
Now, what to do instead? Well, it's really not clear exactly what you want Sumary_Table to contain. Maybe something like what follows. Note that the if/elseif blocks are all doing basically the same thing, so it's easy to generalize them using Pattern_For_List{J} rather than the hard-coded values "_02", "_03", etc. (and no need to tranpose Pattern_For_List either, not for this anyway).
pathn = '.';
F = dir(fullfile(pathn,'*.txt'));
filelist = fullfile({F.folder},{F.name});
N = numel(filelist);
Summary_Table = cell(N,1); % cell array of tables, one per file
for K = 1:N
filename = filelist{K};
file_Details = importdata(filename);
Data_in = readtable(filename, 'Delimiter',{',',';',' '});
is_empty = strcmp(Data_in.Var1,':').';
s_idx = strfind([false is_empty],[false true]);
e_idx = strfind([is_empty false],[true false]);
for ii = 1:numel(s_idx)
Data_in{s_idx(ii):e_idx(ii),1} = Data_in{s_idx(ii)-1,1};
end
All_Para = Data_in.Var1;
Slot_PG_List = {'C_PG_SlotPGID_00';'C_PG_SlotPGID_01';'C_PG_SlotPGID_02';'C_PG_SlotPGID_03';'C_PG_SlotPGID_04';'C_PG_SlotPGID_05';'C_PG_SlotPGID_06'};
Compare_PG_List = extractAfter(Slot_PG_List,"SlotPGID_");
Compare_Slots = contains(All_Para,Slot_PG_List);
Slot_Data = Data_in(Compare_Slots,:);
Slot_Data_Values = lt(Slot_Data.Var4,255);
Slots_Active = Slot_Data.Var1(Slot_Data_Values);
Pattern_For_List = extractAfter(Slots_Active,"SlotPGID");
Extracting_List = find(endsWith(Data_in.Var1,Pattern_For_List));
Extracting_List_Data = Data_in(Extracting_List,["Var1","Var3","Var4","Var7","Var8"]);
NP = numel(Pattern_For_List); % cell array of tables, one per suffix
T = cell(NP,1);
for J = 1:NP
idx = find(endsWith(Extracting_List_Data.Var1,Pattern_For_List{J}));
T{J} = Extracting_List_Data(idx,:);
end
Summary_Table{K} = vertcat(T{:}); % vertically concatenate to make a summary table for file K
end
disp(Summary_Table)
{6150×5 table} {2050×5 table} {6150×5 table} {2050×5 table}
% optionally vertically concatenate to make a summary table for all files
Summary_Table = vertcat(Summary_Table{:});
  6 Commenti

Accedi per commentare.

Più risposte (0)

Categorie

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

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by