Concatenate multiple .csv files horizontally
Show older comments
Dear MATLAB aficionados!
I am trying to horizontally concatenate multiple .csv files produced using Freesurfer (see attached 'Trial_001.csv' and 'Trial_002.csv'). I want to concatenate the values listed after each of the fields (e.g. "10", "mean", "stdev") horizontally into a single .csv file and preserve the file name as the column header for each trial. I have tried the below which outputs the attached 'Compiled.csv'. This however concatanates vertically. When I try using horzcat, it returns "Duplicate table variable name: 'measures'". Beyond manually relabelling every .csv file, I am not sure how to resolve. Also, neither option preserves the file names.
Any advice would be greatly appreciated!
input_path = '/Users/annabellesorby-adams/TRIAL/01_Output_Files' ;
results_path = '/Users/annabellesorby-adams/TRIAL/03_Compiled' ;
output_file = [results_path filesep 'Compiled.csv'];
file_info = dir(fullfile(input_path,'*.csv'));
full_file_names = fullfile(input_path,{file_info.name});
n_files = numel(file_info);
all_data = cell(1,n_files);
for ii = 1:n_files
all_data{ii} = readtable(full_file_names{ii},'PreserveVariableNames',false);
end
all_data{:}
writetable(cat(1,all_data{:}),output_file);
disp 'All done!'
Accepted Answer
More Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!