MATLAB: Creating a loop to define variables for a certain number of files and using these variables to do further operations to these files.

variables in file names

Hey guys,
I want to create variables for a certain number of files which have basically the same name but only differ in the number at the end of their names by using a loop. Further I want to read these files, extract some data and export these data all by using the created file name variables.
Could you maybe have a look on my attached Test.mat file?
Thank you very much for your help! 🙂
Cheers,
Sven

Best Answer

Try something like this. You need to use cell arrays.
d1_file = cell(1,5);
for k = 1:5
d1_file{k} = ['OPTP_1_Scan9-Gr-1750nmpump-280deg-scan Vg-0--3-3-0Vx3_time' num2str(k) '.dat'];
end
data1 = cell(1,numel(d1_file));
for k = 1:numel(d1_file)
fileID = fopen(d1_file{k});
data1{k} = textscan(fileID, '%s %s', 'HeaderLines', 16);
fclose(fileID);
end
result1 = cell(1,numel(d1_file));
for k=1:numel(d1_file)
result1{k} = vpa(data1{k}{2});
end
% to export
for i=1:numel(d1_file)
writecell(data1{k}{2}, ['file' num2str(i) '.xlsx']);
end