Write a cell array to a .csv file

Hi,
I have a cell array "newdata" as shown below.
'01/01/2001' 0
'02/01/2001' 0
'03/01/2001' 0
'04/01/2001' 0
'05/01/2001' 14.1000000000000
'06/01/2001' 0
'07/01/2001' 0
I need to write this to a .csv file. So, I used the following code to do that.
csvwrite('110416.csv',newdata(:,1:2));
But, I got the following error.
Error using dlmwrite (line 112)
The input cell array cannot be converted to a matrix.
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
Can somebody suggest me a different way or how to correct this.?
Thanks in advance.

Respuestas (3)

Geoff Hayes
Geoff Hayes el 25 de Nov. de 2014

2 votos

Damith - see Export Cell Array to Text File which uses fprintf to write each row of your cell array to file.

4 comentarios

Damith
Damith el 25 de Nov. de 2014
Thanks. I need to write the output to a .csv file though.
You can still do that - csv or text file it is more or less the same. For example,
fid = fopen('110416.csv','wt');
if fid>0
for k=1:size(newdata,1)
fprintf(fid,'%s,%f\n',newdata{k,:});
end
fclose(fid);
end
Damith
Damith el 25 de Nov. de 2014
Thanks. This works.
Akhil Narayanan
Akhil Narayanan el 14 de En. de 2022
thanks

Iniciar sesión para comentar.

D. benoy
D. benoy el 26 de Nov. de 2014
Hi,
If you have EXCEL on your computer you can do the following instead
xlswrite('110416.xls',y);
Open 110416.xls and save it as a csv file.

3 comentarios

Damith
Damith el 26 de Nov. de 2014
Thanks. I know. But the previous method is more efficient.
Preston Dial
Preston Dial el 25 de Jul. de 2018
I am getting the same error from the xlswrite function for one of my projects
Devansh Deepak Patel
Devansh Deepak Patel el 12 de Ag. de 2020
me too

Iniciar sesión para comentar.

Preguntada:

el 25 de Nov. de 2014

Respondida:

el 14 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by