MATLAB: Question about ‘save’ option for workspace variable saving

MATLABsaveworkspace

Hello everybody,
I've the follow problem with 'save' function in Matlab:
I would find a method that permit me to save an entire Matlab workspace but with a name coming from a value of a simulation result variable.
For example:
A = 3000;
save ('A');
In this case I got a file saved with A name and not with it's value 3000 as I would.
How can I solve it?
Thank you in advance.
Best regards.

Best Answer

The filename needs to be a string. You can make a string with a variable value in it by using sprintf and providing the variable;
A = 5000;
save(sprintf('My_A_Value_Is_%d',A),'A');
will save a file called 'My_A_Value_Is_5000.mat' with variable A inside of it.
Do note that you may have to change the type specifier - %d displays integers, %f floating points, etc. Your filesystem may not take kindly to having any characters in the filename.