Adding columns to tables
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to loop through a list of file names such that I grab a column from a particular file and put it into the first empty column in a table, which I then write to a text file. What happens instead is that instead of adding the column to the end, it changes every existing column in the table. Code below. Any fixes?
clear
clc
% Names of variables to be sorted into individual files
nNames=["Trunk_Fwd_Tilt";"Trunk_Lat_Tilt";"Trunk_Rotation";"Pelvis_Fwd_Tilt";"Pelvis_Lat_Tilt";"Pelvis_Rotation";"R_HIPFlexANG";"R_HIPAbdANG";"R_HIPRotANG";"R_KNEEFlexANG";"R_KNEEAbdANG";"R_KNEERotANG";"R_ANKFlexANG";"R_Foot_Orientation";"R_Shl_Flex_Ang";"L_HIPFlexANG";"L_HIPAbdANG";"L_HIPRotANG";"L_KNEEFlexANG";"L_KNEEAbdANG";"L_KNEERotANG";"L_ANKFlexANG";"L_Foot_Orientation";"L_Shl_Flex_Ang";"R_KNEEPWR";"R_ANKPWR";"L_HIPPWR";"L_KNEEPWR";"L_ANKPWR"];
pName=["Trunk Tilt","Trunk Obliquity","Trunk Rotation","Pelvic Tilt","Pelvic Obliquity","Pelvic Rotation","Hip Flexion/Extension","Hip Ad/Abduction","Hip Rotation","Knee Flexion/Extension","Knee Varus/Valgus","Knee Rotation","Ankle Dorsi/Plantar","Foot Progress wrt Room","Shank Rotation"];
% Scan for names of all norm files
direc=["EXAMPLE NAME","EXAMPLE NAME","EXAMPLE NAME"];
fileID=fopen(EXAMPLE NAME,'w');
pasteTable=table();
%Make file containing all the file names we want from each directory
for tInc=20:30
sFind=strcat("_hss_",num2str(tInc));
for dirInc=1:3
dirInfo=dir(direc(dirInc));
structSize=(size(dirInfo));
for nInc=1:structSize(1)
dirName=dirInfo(nInc).name;
pHold=string(dirName);
if contains(pHold,sFind)
%Append folder name to file name
pHold=strcat(direc(dirInc),pHold);
fprintf(fileID,'%s\n',pHold);
end
end
end
end
%Read the table back in
opts=delimitedTextImportOptions('LineEnding','\n');
dirNames=readtable('EXAMPLE NAME',opts);
%Increment through file names
for nInc=1:height(dirNames)
dest=dirNames.Var1(nInc);
dest=char(dest);
[nOutTable,topCol]=Norm2Tab(dest);
%Incrementing through all names of variables
for i=1:length(nNames)
vName=nNames(i);
fName=strcat('EXAMPLE NAME',vName,'.txt');
fileID2=fopen(fName,'w');
%Incrementing through all file names
for j=1:height(dirNames)
pHold=nOutTable(1:100,vName);
cName=char(strcat("Var"+j));
pHold.Properties.VariableNames={cName};
pasteTable=[pasteTable pHold]
end
writetable(pasteTable,fName,'WriteVariableNames',false);
end
end
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 18 Jul. 2019
Bearbeitet: Adam Danz
am 18 Jul. 2019
Try saving all tables to a cell array and then concatenating them after the loop.
%Incrementing through all file names
C = cell(1,height(dirNames));
for j=1:height(dirNames)
pHold=nOutTable(1:100,vName);
cName=char(strcat("Var"+j));
pHold.Properties.VariableNames={cName};
C{i} = pHold; % Each column must have a unique variable name!
end
pasteTable = [C{:}]; % Concatenate
Note: I didn't comb through your code to figure out why your current method is not working. Any existing problems may also affect the method I recommended. If the problem persists, please attach the necessary files so we can run your code.
2 Kommentare
Weitere Antworten (1)
David K.
am 18 Jul. 2019
Check out https://www.mathworks.com/help/matlab/matlab_prog/add-and-delete-table-variables.html if you have not.
It has many ways that you can add columns to a table. Picking out a few that I think may be useful to you-
If you want to replace or make a new column and you know the name of the column you can do
Table.ColName = ColMatrix;
If you want to append the columns to the end you can do it like this
newCol = table(ColMatrix);
Table = [Table newCol];
0 Kommentare
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!