0

I'm currently working on a study where the data is saved in .txt files for each participants. I'm looking for a way to load all the data from each files into matlab automatically. I've found many topic on loading multpile files, but i can't find a way to make it work for me.

I want to load all the files called "Log_*" into a table that goes like that:

delim = {')(',') ','(', ' ', ','};
T = readtable('Log_***.txt','Delimiter', delim,'MultipleDelimsAsOne',1);
T = removevars (T, {'Var1', 'Var2', 'Var4', 'Var6', 'Var8', 'Var10', 'Var12','Var14', 'Var16', 'Var18', 'Var26', 'Var27', 'Var31', 'Var36', 'Var40', 'Var45', 'Var47', 'Var49'});
newnames = {'essai', 'DureeEssai', 'azVoulue', 'elVoulu', 'azMesure','elMesure', 'distanceAngulaire', 'diffhorizontale', 'diffElevation', 'x1', 'y1', 'z1', 'x2', 'y2', 'z2', 'posTx', 'posTy', 'postz', 'oriTx', 'oriTy', 'oriTz', 'oriTw', 'posCx', 'posCy', 'posCz', 'oriCx', 'oriCy', 'oriCz','oriCw','distanceAngulaire_oriT_C','az_oriT','el_oriT'};    
T.Properties.VariableNames = newnames;
T = T(2:2:end,:);

This works for one file at a time, but i can't manage to write a proper script to get it done for every file at once. If you know the way, i'd be glad if you could share it.

Cheers

Saqib Shahzad
  • 982
  • 12
  • 28
  • 2
    You will have to use [dir](https://www.mathworks.com/help/matlab/ref/dir.html) and loop over the files – Paolo Mar 19 '19 at 11:01
  • There is an example in [Loop through files in a folder in matlab](https://stackoverflow.com/q/11621846/5358968) – Steve Mar 19 '19 at 11:02
  • Thanks a lot for your help! I now have this: `files = dir('Log_*.txt'); for file = files' txt = importdata(file.name); delim = {')(',') ','(', ' ', ','}; txt = cellfun(@(x)strsplit(x,delim),txt,'UniformOutput',false); txt = cat(1,txt{:}); end` But it only loads the number of row that every files have (67). I've also tried the script with k= 1:length(xx) but i get the same result. – Nocturne Mar 19 '19 at 16:28

1 Answers1

0

Using dir and matrix concatenation, you can append one row of a table to the end of another table:

T = table;
list_of_files = dir(**directory of files**);
for i = 1:length(list_of_files)
    filename_to_use = list_of_files(i).name;
    U = table;

    **read file in and place data into table**

    T = [T; U];
    clear U;
end
disp(T);

This is if you want all your data to be stored in one table.

medicine_man
  • 321
  • 3
  • 15