Search code examples
matlabsavefilenames

How to save using a default or dynamically generated filename in Matlab?


How to save using a default or dynamically generated filename in Matlab? I have done something like this below. But my .dat file is saved as filename.dat instead of the filename I generated using sprintf command.

filename = sprintf('k2-%d_k3-%d_k4-%d.dat', K2,K3,K4) 
save filename variable -ascii

Here, %d would be replaced by the numbers of those variables K2,K3,K4 in my filename.


Solution

  • Thanks. I have found the solution.

    save(filename, 'variable', '-ascii')
    

    does the trick. I had been searching for a while, and it clicks only after I post.