Adding columns to tables

28 ビュー (過去 30 日間)
IE
IE 2019 年 7 月 18 日
コメント済み: Adam Danz 2019 年 7 月 18 日
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

採用された回答

Adam Danz
Adam Danz 2019 年 7 月 18 日
編集済み: Adam Danz 2019 年 7 月 18 日
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 件のコメント
IE
IE 2019 年 7 月 18 日
As it turned out, I'd hardcoded the file name in Norm2Tab, so it ran that regardless of the file destination I put in, but this method did work once I fixed it. Thanks!
Adam Danz
Adam Danz 2019 年 7 月 18 日
Good find! Glad I could help.

サインインしてコメントする。

その他の回答 (1 件)

David K.
David K. 2019 年 7 月 18 日
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];

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by