How to write multiple input into one csv file

97 views (last 30 days)
Let say I have
a=1;
b=2;
c=3;
d=4;
I want to put this output value in matlab to a csv file in the order below.
ff.PNG
I can use below, However, it will over-write the csv file.
>> csvwrite('1test.csv',a)
>> csvwrite('1test.csv',b, 0,1)
>> csvwrite('1test.csv',c, 0,2)
>> csvwrite('1test.csv',d, 0,3)
Thanks!

Answers (2)

Arvind Sathyanarayanan
Arvind Sathyanarayanan on 18 Feb 2019
Try using the dlmwrite command with '-append'. Syntax is as follows:
dlmwrite(filename,M,'-append')
The other option is to store a,b,c,d in a array and then write that array to the csv file.
  3 Comments
Matlaber
Matlaber on 18 Feb 2019
Thanks!
I think need time to figure out it.

Sign in to comment.


Mohammad Farhad Aryan
Mohammad Farhad Aryan on 12 Mar 2020
Use the following code:
a = 1;
b = 2;
c = 3;
d = 4;
Nums = [a, b, c, d];
writematrix(Nums, 'Nums.csv')
  2 Comments
Walter Roberson
Walter Roberson on 12 Mar 2020
The original poster wanted to be able to write incrementally, doing separate write operations each of which appended a column of data. writematrix() can only do that for "spreadsheet" (.xls, .xlsx) files and not for "text" files (such as .csv), and writematrix() needs to be told exactly where to write the data each time.
The only system of calls that comes close to providing an "append into next column" operation is directly using ActiveX to talk to Excel, in which case certain kinds of range selections automatically update to position themselves after data that is written. Otherwise your choices are the ones I described at https://www.mathworks.com/matlabcentral/answers/445664-how-to-write-multiple-input-into-one-csv-file#comment_672637

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!