MATLAB: How to save the value, a string, of a variable as a variable name of an array

arrayfilesavestringvariable

Hello,
I have a variable and a double array :
s_fieldnames = 'aaa';
a_nums = [1, 2, 3, 4, 5, 6];
I would like to save this into a mat file. To be clearer, i would like to have a mat file which contains :
aaa = [1, 2, 3, 4, 5, 6];
It means I would like to have the value of s_fieldnames as the name of the double array. So that if i look into my file I will have aaa which contains the double array.
After that I want to use the function save.
save('filename', ...);
Thank you Adrien

Best Answer

Hi,
something like
s_fieldnames = 'aaa';
a_nums = [1, 2, 3, 4, 5, 6];
% create the variable aaa containing the values of a_nums
eval([s_fieldnames '=a_nums;']);
% save it in a mat file
save('filename',s_fieldnames)