automatically iterate numbers in a text file

3 Ansichten (letzte 30 Tage)
avram alter
avram alter am 12 Dez. 2019
Kommentiert: avram alter am 24 Dez. 2019
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.

Akzeptierte Antwort

Guillaume
Guillaume am 12 Dez. 2019
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);
  8 Kommentare
Guillaume
Guillaume am 24 Dez. 2019
"do you know what is causing this?"
Nothing to do with my code. It's your overloading of the variable file, first used to store the filename, then used to store the content of the file that you imported with importdata. It's your addition of the line:
file = importdata(file);
As for the regex:
  • (?<=Se) means that a match must be preceded by Se
  • (\d+) is the first token captured. It matches 1 or more digits. Meant to match the number after Se
  • (.*) is the 2nd token captured. It matches anything except a new line. Meant to match the rest of the line before:
  • (\d+) third token captured. It matches 1 or more digits. Meant to match the number at the end of the line because:
  • (?= *([\n\r]|$) match must be followed by any number of space (the space followed by *) followed by either \n or \r (windows/linux newlines) or the end of the text (for the last line).
avram alter
avram alter am 24 Dez. 2019
I'm not sure when I included that line. Possibly when I ran it on a new system. Either way, got it up aan running without a hitch, thank you for all the help. Fantastic explanation as well.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Language Support finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by