How do I save an matrix with a string name and save it?

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');

 Accepted 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

4 Comments

@Ivy Chen: as Robert U clearly states, dynamic variable names are not recommended. Read this:
You should use the solution that Robert U gives above. Actually it is much easier to process data using a loop and .mat files if the variable name/s are exactly the same in each .mat file, because it makes the code much simpler and more efficient.
Thanks! After submitting the question yesterday, I worked out something using followings, and it seems to ahieve what I want. I also clear the data matrix at end of loop to avoid overloading .mat files.
However, I am wondering if using the assignin feature will cause any issue(s).
DUT_name = regexprep(regexprep(filename, '\s+', ''),'UE_rec.csv','');
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'});
assignin('base', sprintf(DUT_name), DUT);
%%Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
Save loaded data file in workspace
save(sprintf(DUT_name), sprintf(DUT_name));
clear(sprintf(DUT_name));
"However, I am wondering if using the assignin feature will cause any issue(s)."
Yes, assignin will make your code slow, complex, increases the risk of bugs, and makes debugging harder. You are dynamically creating variables, which is specifically what Robert U and I recommended against doing. Read the link I gave in my comment to know more about why this should be avoided, and some much better alternatives (such as using dynamic fieldnames or indexing).
"I also clear the data matrix at end of loop to avoid overloading .mat files."
What does that mean? Clearing variables like this is usually not required in MATLAB, as MATLAB has very good memory management that takes care of things like this.
Appreciate your feedback on this. Will update my codes with what Robert U. and you recommended in the earlier reply. Thanks again.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Jul 2018

Edited:

on 24 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!