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!