datvar
to a file mydata.dat, all you have to do is say
save mydata.dat datvar
However if you want to dynamically generate your filename, things get a little tricky since MATLAB's documentation is sparse about this. Here's how:
% Create a filename (myfile.001) using sprintf
fOut = sprintf('myfile.%03d',i); % say, i=1
% save data as an ASCII file
save(fOut, 'datvar', '-ascii');
% Or, create a mat file - (myfile001.mat)
% since MATLAB binary files typically have the extension .mat
fOut = sprintf('myfile%03d.mat',i); % say, i=1
% save data as matlab binary
save(fOut, 'datvar');
If you have multiple columns of data, just ensure that
datvar
is an array. Remember that the .mat file will retain the name of the variable datvar
but the ASCII file will only contain data.