MATLAB: Automatically iterate numbers in a text file

MATLABtextscan

I have a text file that looks like this:
LOAD BOX 1 SUBJ M1_299633_D295158_JUN19@1910_Aut_ERROR2 EXPT St6_Se4_Rat1 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 3
LOAD BOX 2 SUBJ M2_297928_D294277_APR19@1910_Aut_ERROR2 EXPT St6_Se4_Rat2 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 3
LOAD BOX 3 SUBJ M3_299632_D295158_JUN19@1910_Aut_ERROR2 EXPT St6_Se4_Rat3 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 3
LOAD BOX 4 SUBJ M4_297929_D294277_APR19@1910_Aut_ERROR2 EXPT St6_Se4_Rat4 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 3
LOAD BOX 5 SUBJ F5_299621_D295158_JUN19@1910_Aut_ERROR2 EXPT St6_Se4_Rat5 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 3
LOAD BOX 6 SUBJ F6_297923_D294277_APR19@1910_Aut_ERROR2 EXPT St6_Se4_Rat6 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 3
LOAD BOX 7 SUBJ F7_299626_D295158_JUN19@1910_Aut_ERROR2 EXPT St6_Se4_Rat7 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 3
LOAD BOX 8 SUBJ F8_297924_D294277_APR19@1910_Aut_ERROR2 EXPT St6_Se4_Rat8 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 3
I want to make a matlab code that will pull a file from a directory, and update the file by iterating by 1 the number after Se on each line (session number), as well as the final number (list number), also iterating by one.
the files are saved under this general format:
SquadNumber_InternalCode_ExperimentName_oldStage_OldSession.txt
the way I envision this is that matlab will pull in the outdated file, see the number 4 as the session number, make the 4 into a 5. it will do the same with the list number. this is the code I have to get some of the information for the creation of the file:
root = 'C:\Users\Administrator\Used_files';
user_input_squad = input('please enter squad number: ');
user_input_experiment = input('please enter experiment name: ');
file = fullfile(root, sprintf('squad%s_1910_%s_St6_Se4.txt', user_input_squad, user_input_experiment));
Used_file = importdata(file);
I want to make a code that will update all instances of Se4 in the file to Se5, every day, as well as update the name of the file to Se5.
I also want the last number to be iteratede by one, but in this case only go up to 5, and then start back at 1.
thanks for the help, I appreciate it.

Best Answer

root = 'C:\Users\Administrator\Used_files';
user_input_squad = input('please enter squad number: ');
user_input_experiment = input('please enter experiment name: ');
file = fullfile(root, sprintf('squad%s_1910_%s_St6_Se4.txt', user_input_squad, user_input_experiment));
%get raw content of file as text
filetext = fileread(file);
%increase number after Se by 1, and increase last number modulo 5.
newtext = regexprep(filetext, '(?<=Se)(\d+)(.*)(\d+)$', ...
'${num2str(str2double($1)+1)}$2${num2str(mod(str2double($3), 5)+1)}', ...
'lineanchors', 'dotexceptnewline');
%generate new file name
newfile = fullfile(root, sprintf('squad%s_1910_%s_St6_Se4.txt', user_input_squad+1, user_input_experiment));
%and write new text
fid = fopen(newfile, 'w');
fwrite(fid, newtext);
fclose(fid);