MATLAB: How to save a series of variables in workspace to a txt file

for loopfprintftxt

Hi all,
I want to save a bunch of variables (data1, data2 etc) to a textfile (alldata.txt). The code below is a simplified version but it makes the problem clear, since the saved data in the textfile is not correct.
data1 = 1
data2 = 2
data7 = 3
data10 = 5
for i = 1:10
try
fid = fopen('alldata.txt','w');
x = (sprintf('data%d', i));
fprintf(fid,'%d\n',x);
fclose(fid);
catch
continue;
end
end
Tthis is the output in alldata.txt, which is wrong:
100
97
116
97
49
48
Is there anybody who can help me?
ps. I dont want to make use of the option: save() with the -ascii flag, because I want to be able to do some text formatting.