Home > Net >  How to save and reload a .mat file with a changing name (e.g. 1,2,3) and constant suffix (e.g. AB)
How to save and reload a .mat file with a changing name (e.g. 1,2,3) and constant suffix (e.g. AB)

Time:08-26

I need help on my code below

path(path,'E:\DataAnalysis')
file_name='1'
data=eval(strcat('readtable("',file_name,data_type,'");'))
% Mainbody of mo code to process data which is perfectly fine
eval(strcat('save(''',file_name,'AB.mat''',')'))
load(file_name, '-AB');

The code above is perfectly fine with reading my Excel file with numbers as the file_names (1,2,..). After processing it will be saved into .mat file with same file_name (1,2) and added suffix of AB. the path will be same dir as my code.

Now I need help on:

  • Firstly, how to define a different saving dir.
  • Secondly, how to have a code to load the .mat file after it is being saved. So I do not have to read whole code again and again. But the thing is I dont want to give a name manually. I want to load the same file_name which saved with suffix "AB".

CodePudding user response:

Number 1, and most important: remove eval.

try this:

my_dir= 'C:/somewhere/';
for ii=1:10
   save([my_dir, 'hello_number_', str(i),'.mat'],ii)
end
  • Related