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

7 次查看(过去 30 天)
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');

采纳的回答

Robert U
Robert U 2018-7-24
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 个评论
Stephen23
Stephen23 2018-7-24
编辑:Stephen23 2018-7-24
"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 2018-7-24
Appreciate your feedback on this. Will update my codes with what Robert U. and you recommended in the earlier reply. Thanks again.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by