Loop through excel files to transpose data and write to single file
이전 댓글 표시
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
Siddharth Bhutiya
2021년 5월 30일
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
답변 (1개)
Walter Roberson
2021년 5월 30일
T = readtable(files(k).name);
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!