Adding a new column to an existing csv file via Matlab.

Hello,
I have a question. I want to add a new column to an existing filename via commants in matlab. The existing file (called 'filename1' ) has 22 columns and 400 rows. I ve created the column that I want to add , whis has 400 rows too. The final output i want to have is a csv file with 23 columns and 400 rows. I used these commands , without results:
M1 = [filename1, num2cell(d1(1:end,7))].'; %transpose important
fid = fopen('gtm.csv','wt');
fprintf(fid,'%15s %.6f\n',M1{:} );
fclose(fid);
the "num2cell(d1(1:end,7))" is the new column that i want to add.
Thanking in advance

 Accepted Answer

You can do it easily with writetable.
Here is an example.
filename1 = 'existingData.csv';
% Read the CSV as a table
t = readtable(filename1);
% Add a new column to the end of the table
numOfColumn = size(t, 2);
newCol = num2cell(d1(1:end,7)); % Your new column
t.(numOfColumn+1) = newCol;
% Change column name if needed
t.Properties.VariableNames{numOfColumn+1} = 'newCol';
% Write to CSV file
writetable(t, 'new.csv')

6 Comments

Or you could simplify a bit by using
filename1 = 'existingData.csv';
% Read the CSV as a table
t = readtable(filename1);
% Add a new column to the end of the table
t.newCol = d1(1:end,7); % Your new column
% Write to CSV file
writetable(t, 'new.csv')
WIth R2019a or later in the special case that the rows are all numeric, then
filename1 = 'existingData.csv';
% Read the CSV as an array
t = readmatrix(filename1);
% Add a new column to the end of the array
t(:,end+1) = d1(1:end,7); % Your new column
% Write to CSV file
writematrix(t, 'new.csv')
Walter Roberson, Kojiro Saito Thank you for your answers but there is a problem...
ok, I am using these commands :
fid = fopen('results.txt','wt');
writetable(CT{:},'tabledata.txt')
fclose(fid);
and in the command window shows these:
Error in writetable (line 106)
write(a,filename,varargin{:})
Error in test_v2_pro (line 118)
writetable(CT{:},'tabledata.txt')
Do you know something about?
You would not fopen: writetable does its own file operations.
You should not be passing CT{:} and instead should be passing a table object. Not a cell array.
Walter Roberson,Excuse me , but honestly I do not understand what do you mean by saying:
"You should not be passing CT{:} and instead should be passing a table object. Not a cell array."
Could you explain me ?
Walter Roberson,Basically i believe that the main problem is in the command writetable, because command window shows me these:
Undefined function 'write' for input arguments of type 'cell'.
Error in writetable (line 106)
write(a,filename,varargin{:})
Error in test_v2_pro (line 103)
writetable(MI{:},'tabledata.txt')
Do you have any idea about the command should I use in order to finish it with success?
We are telling you that you should stop working with xlsread and should use readtable() to read your data, and use the command we show to add a new column to the table and then writetable the results.
%read file
t = readmatrix('filename1.csv');
% Add a new column to the end of the array
t(:,end+1) = d1(:,7); % Your new column
% Write to CSV file
writematrix(t, 'tabledata.txt')
Notice no xlsread, no cell array.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!