MatLab-->csv: Export data to csv file using dlmwrite in Matlab

I was looking for how to trying to save some data into a csv file using Matlab and I found something that was useful. I use the data and code:

data

A = [1 2 3 4 5 6 7 8];
names={'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'};

code

save('Test','names','A');
nomFile='Test.csv';
fid=fopen(nomFile,'w');
fprintf(fid,'%s,',names{:});
 fprintf(fid,'\n');
dlmwrite('Test.csv',A,'-append');

fclose(fid);

answer

a,b,c,d,e,f,g,h,
1,2,3,4,5,6,7,8

but it only saves the data, the way I want if I don't specify any folder. So it will be saved on the same folder where the script is located. But I want to save it in another folder and it only gives the numbers part.

1,2,3,4,5,6,7,8

Any one know how can I put something like

dlmwrite('C:\Data_Analysis\Test.csv',A,'-append');

?

I'd appreciate for your help.

Bruno

PS: sorry, I still don't know how to put colors in the code to make it more clear

Upvotes: 2

Views: 1674

Answers (1)

Shai
Shai

Reputation: 114866

I think the problem is that you write the names to a file at the local folder and not the other folder.

fldr = fullfile('c:','Data_Analysis');
nomFile=fullfile( fldr, 'Test.csv' );
fid=fopen(nomFile,'w');
fprintf(fid,'%s,',names{:});
fprintf(fid,'\n');
dlmwrite(nomFile,A,'-append');
fclose(fid);

Upvotes: 2

Related Questions