MATLAB: How to save an matrix with a string name and save it

dynamic filenamedynamic variable namesave

I need to load in some .csv files and save both data matrix and the .mat file using the modified string. Following is my code, but I received a "You cannot subscript a table using only one subscript. Table subscripting requires both row and variable subscripts." error.
CSVfiles = dir('*UE_rec.csv');
for i=1:length(CSVfiles)
filename=CSVfiles(i).name;
delimiter = ',';
startRow = 2;
formatSpec = '%q%f%q%f%f%f%f%f%q%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%q%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
DUT_name = regexprep(regexprep(filename, '\s+', ''),'UE_rec.csv','');
sprintf(DUT_name)=table(dataArray{1:end-1}, 'VariableNames', {'Scenario','index','date','latitude','longitude','altitude','heading','speed','vert_speed','HDOP','VDOP','PDOP','GDOP','TDOP','EHE','EVE','True_HE','True_VE','FOM','C_N01','C_N02','C_N03','C_N04','C_N05','C_A_code_tracks','C_A_carrier_tracks','PY_L1_code_tracks','PY_L1_carrier_tracks','PY_L2_code_tracks','PY_L2_carrier_tracks','PL_carrier_tracks','Num_SV','Tracking'});
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
save(sprintf(DUT_name));
If I replace the last 3 lines with followings, it will save all the .mat files accordingly, but everyone will have the data matrix with the same name as "DUT", instead of matching the filename.
DUT=table(dataArray{1:end-1}, 'VariableNames', {'Scenario','index','date','latitude','longitude','altitude','heading','speed','vert_speed','HDOP','VDOP','PDOP','GDOP','TDOP','EHE','EVE','True_HE','True_VE','FOM','C_N01','C_N02','C_N03','C_N04','C_N05','C_A_code_tracks','C_A_carrier_tracks','PY_L1_code_tracks','PY_L1_carrier_tracks','PY_L2_code_tracks','PY_L2_carrier_tracks','PL_carrier_tracks','Num_SV','Tracking'});
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
save(sprintf(DUT_name),'DUT');

Best Answer

Hi Ivy Chen,
dynamic variable names are not a recommended way to go.
One way would be to use something like that:
OUT.(DUT_name) = zeros(10);
save(sprintf('%s',DUT_name),'-struct','OUT')
Kind regards,
Robert