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

3 views (last 30 days)
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

Robert U
Robert U on 24 Jul 2018
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
Stephen23
Stephen23 on 24 Jul 2018
Edited: Stephen23 on 24 Jul 2018
"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.
Ivy Chen
Ivy Chen on 24 Jul 2018
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)

Community Treasure Hunt

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

Start Hunting!