MATLAB: How to save a string as a filename and only include specific variables


Hi, I'm trying to generate a .dat file, with a filename specified by the user, only including one variable from the file, an array titled A. I've managed to either save the .dat with the user-specified filename, or to save the .dat with a given filename and only including the array, but not the two at the same time.
To get a variable filename, I followed the advice here: and typed
name = 'Enter filename: ';
file = input(name,'s');
save (file);
That generates a file with the name input (e.g. myfile.dat), but the contents are garbled.
To get a .dat file with the contents of A, I typed
save -ascii MatrixA.dat A;
That gives a .dat containing the values of A as required, but doesn't let the user change the filename. I've tried combining the two segments in various ways, but haven't managed to find any that work yet. I'd really appreciate any guidance!

Best Answer

  • save(file,'-ascii','A')
    Call save with the variables inside of () rather than with spaces where they represent strins rather than values.