Loop through excel files to transpose data and write to single file

5 views (last 30 days)
I'm trying to loop through about 80 excel files, each with the same number of rows but varying number of columns. Each file name has a unique ID number beginning with 'U', and a unique timestamp. I need to transponse the data, and then write all data to a single file, but so far I haven't had any success with the looping.
This is the syntax I run on a single file:
file='u12345_HRV Analysis_8_55_08 PM.xlsx';
[filepath,name,ext] = fileparts(file);
T = readtable(file);
aTableArray = table2array(T);
aTableT = array2table(aTableArray.');
writetable(aTableT,name,'WriteVariableNames',0,'delimiter','\t');
This is how I've tried adapting it to a loop:
files = dir('*.xlsx');
for k=1:length(files)
T = readtable(files{k});
aTableArray = table2array(T);
aTableT = array2table(aTableArray.');
writetable(aTableT,files.name,'WriteVariableNames',0,'delimiter', '\t')
end
But I can't seem to get past T = readtable(files{k}); I get an error stating "Brace indexing is not supported for variables of this type." When I try it with (), I get "Input must be a row vector of characters or string scalar."
Thanks so much!
  1 Comment
Siddharth Bhutiya
Siddharth Bhutiya on 30 May 2021
You could use rows2vars instead of converting your table to an array transposing it and then converting back to write it to a file.
https://www.mathworks.com/help/matlab/ref/rows2vars.html

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 30 May 2021
T = readtable(files(k).name);

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!