Hi! I need help regarding merging multiple CSV files into single file. At the first time, I managed to run this code :
%%Merge multiple CSV files into one CSV file
myDir = uigetdir % gets directory from any folder
d=dir(fullfile(myDir,'*.csv')); % retrieve the files
fido=fopen(fullfile('finalCSVnew.csv'),'w'); % open output file to write
for i=1:length(d) fidi=fopen(fullfile(myDir,d(i).name)); % open input file
fwrite(fido,fread(fidi,'*char')); % copy to output
fclose(fidi); % close that input file
endfido=fclose(fido); clear fid* d % close output file, remove temporaries
Turns out I have to change the command for "myDir" so it can select multiple file in one folder, not all file in one folder which need to be processed. So I change the code above to:
%%Merge multiple CSV files into one CSV filemyDir = uigetfile('*.csv','Select the data file','MultiSelect','on'); % gets directory from any folderd=fullfile(myDir,'*.csv'); % retrieve the filesfido=fopen(fullfile('finalCSVnew.csv'),'w'); % open output file to writefor i=1:length(d) fidi=fopen(fullfile(myDir,d(i).name)); % open input file fwrite(fido,fread(fidi,'*char')); % copy to output fclose(fidi); % close that input fileendfido=fclose(fido); clear fid* d % close output file, remove temporaries
and there is an error message "Struct contents reference from a non-struct array object." Could you please help me to solve the problem? Thank you very much.
Best Answer