How do I save an matrix with a string name and save it?
Show older comments
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
More Answers (0)
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!