Loop through excel files to transpose data and write to single file
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Siddharth Bhutiya
am 30 Mai 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
Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Import from MATLAB 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!