How do I add rows to a table in a for loop?
53 views (last 30 days)
Show older comments
I want to figure out how to take data from a list of tab-delimited .txt files of indeterminate length with formats like this:
Test Number Result
First test 1
First test 8
First test 3
First test 4
Test Number Result
Second test 201
Second test 208
Second test 203
Second test 204
And produce a table like this:
First 1 8 3 4
Second 201 208 203 204
filePattern = fullfile(myFolder, '*.txt'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
usableTable = readtable (fullFileName);
choppedTable = usableTable(:,["Test","Result"])
punyTable = choppedTable (1, "Test")
punyTable
SecondChopTable = choppedTable(:, "Result")
LongRow = rows2vars(SecondChopTable)
NewRow = [punyTable, LongRow]
NewRow.OriginalVariableNames = []
NewRow = vertcat(NewRow{i,:});
end
My problem is that, try as I might, I can't get NewRow to add rows onto the end of a table.
3 Comments
Stephen23
on 25 Jan 2023
Edited: Stephen23
on 25 Jan 2023
"addpath '\\myfiles.campus.edu\alister\Sandbox' %adds where the files are"
Do not add folders of data files to the MATLAB search path. The search path should be for code only.
"myFolder = '\\myfiles.campus.edu\alister\Sandbox' % creates new folder"
That line of code does not create any folder.
"some kind of wonky BS that I don't understand. What I think is happening here is that this is somehow turning this into a real table function (whatever this means)"
The READTABLE documentation explains what it does. The phrase "real table function" exists only on this thread.
Answers (2)
Star Strider
on 23 Jan 2023
Try something like this —
T1 = array2table(magic(5))
for k = 1:3
NewRow = array2table(rand(1,5));
T1 = vertcat(T1,NewRow);
end
T1
.
4 Comments
Star Strider
on 24 Jan 2023
I can’t demonstrate that easily here, however the general guidance is in: Import or Export a Sequence of Files
I would import all of them to a cell array in a loop, as I did here manually (since the directory structure here is a bit more complex than would usually be encountered) and then use the approach I used here to process the data to produce the desired final result.
.
Stephen23
on 25 Jan 2023
Edited: Stephen23
on 25 Jan 2023
"... it keeps producing the tables I want, but not in the order I want, and I don't know how to fix this."
My guess (in lieu of your explanation) is that your filenames are numbered and you expect the file to be returned in numeric order. In that case you will need to sort the filenames alphanumerically. One way is to download my FEX submission NATSORTFILES, which was written to sort filenames into alphanumeric order:
In most cases it is very simple to include in your code:
P = '.'; % absolute/relative path to where the files are saved.
S = dir(fullfile(P,'*.txt'));
S = natsortfiles(S); % download here: https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F);
S(k).data = T(:,["Test","Result"]);
end
T = vertcat(S.data);
[U,~,X] = unique(T.Test,'stable');
H = @(a) {num2cell(a.')};
C = splitapply(H,T.Result,X);
C = [U,vertcat(C{:})]
T = cell2table(C)
Your description is unclear about what data type you require the output to be, so I showed a few options for you.
0 Comments
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!