MATLAB: How to combine these .csv files

column headercombinecsvtables

I am trying to combine two .cvs files into one. The script I have so far works to write the tables to Excel files…
[Timestamps,Samples] = Nlx2MatCSC('C:\Users\HomePC\Desktop\CSClfp1.ncs',[1 0 0 0 1],0,1,[]); % imports CSC.ncs into MATLAB
Samples = Samples';
Timestamps = Timestamps';
Samples = Samples(:,256);
Samples = table(Samples);
writetable(Samples,'./Samples.csv');
Timestamps = table(Timestamps);
writetable (Timestamps, './Timestamps.csv');
This effectively writes the matrices to two separate .csv files. However, it is somewhat annoying to copy the single column of data from Timestamps.csv and paste it into Samples.csv every single time.
I tried adding this:
csv1 = csvread('C:\Users\HomePC\Desktop\Samples.csv');
csv2 = csvread('C:\Users\HomePC\Desktop\Timestamps.csv);
allCsv = [csv1;csv2]; % Concatenate vertically
csvwrite(SamplesandTimestamps, allCsv);
at the end of the script, but MATLAB returns the following errors:
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> Samples\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
Error in NCStoExcel (line 25)
csv1 = csvread('C:\Users\HomePC\Desktop\Samples.csv');
I understand csvread only reads numeric data, but I can't use
Samples.Properties.VariableNames(1) = {1};
I think this does not work because it is not writing "1" as an integer.
Anyone know how I could solve this? Sorry for the length of the post!

Best Answer

You did not tell us, what the contents of Timestamps and Samples is. Maybe it is much easier to concatenate the data before creating the files:
writetable(cat(2, Timestamps, Samples), 'AllData.csv');
Otherwise you can import both files as text:
csv1 = strsplit(fileread('C:\Users\HomePC\Desktop\Samples.csv'), '\n');
csv2 = strsplit(fileread('C:\Users\HomePC\Desktop\Timestamps.csv'), '\n');
both = strcat(csv1, ', ', csv2); % Or which separator you need
fid = fopen('C:\Users\HomePC\Desktop\AllData.csv', 'W');
fprintf('%s\n', both{:});
fclose(fid);