Search code examples
matlabeval

Error by using eval in function in MATLAB


I have the following MATLAB function:

function getDBLfileL1(pathInput,Name_file,folderName)
   DBL_files=dir([pathInput,'/*.DBL']); %get DBL files 
   fprintf('Reading DBL files ... ')
   for i = 1:length(DBL_files) %loop through all DBL files
       [HDR, CS]=Cryo_L1b_read([pathInput,'/',DBL_files(i).name]); %read data with ESA's Cryo_L1_read function
       Coord{i}.LAT_20Hz=CS.GEO.LAT; %store values to struct
       Coord{i}.LON_20Hz=CS.GEO.LON; 
       Coord{i}.BoundingBox_StartLATLON_StopLATLON=[HDR.START_LAT*10^-6,HDR.START_LONG*10^-6,HDR.STOP_LAT*10^-6,HDR.STOP_LONG*10^-6];
       Coord{i}.FileName=[pathInput,'/',DBL_files(i).name];
   end
   eval([Name_file '= Coord;']);
   save(['output/',folderName,'/',Name_file,'.mat'],Name_file,'-v7.3')
   fprintf('done\n')
end

And it is called in the following:

getDBLfileL1(pathInput,[folderNames{i},'_',folderNames1{j}],folderNames{i}); %read Data from DBL file

The Value of Name_file is '2011_01', and I get the following error:

eval([Name_file])
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.

Does anyone know why this error occur or how I can change the file, that I can replace the eval() function?

Thanks a lot in advance!


Solution

  • If I got it right, you are trying to evaluate '2011_01= Coord;' , which means that you are assigning Coord into a variable called 2011_01, and variable names cannot start with numbers