MATLAB Answers

automatically iterate numbers in a text file

2 views (last 30 days)
avram alter
avram alter on 12 Dec 2019
Commented: avram alter on 24 Dec 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.

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 12 Dec 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 Comments

Show 5 older comments
avram alter
avram alter on 23 Dec 2019
I am getting the error
filename must be a string or scalar vector
I assume that is because I added the extension to the name of the file. I have added my full code here, please note I am using the earlier version of hte code, having rid myself of those spaces at the end of the line.
root = 'C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\Completed_Macros';
user_input_squad = input('please enter squad number: ', 's');
user_input_experiment = input('please enter experiment name: ', 's');
user_input_stage = input('please enter stage: ', 's');
user_input_session = input('Please enter session: ', 's');
file = fullfile(root, sprintf('squad%s_1910_EXP%s_St%s_Se%s.mac', user_input_squad, user_input_experiment, user_input_stage, user_input_session));
file = importdata(file);
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_EXP%s_St%s_Se%s', user_input_squad, user_input_experiment, user_input_stage, user_input_session+1));
%and write new text
fid = fopen(newfile, 'w');
fwrite(fid, newtext);
fclose(fid);using
using breakpoints after importfile shows
file is 1x 1 struct with 2 fields
data [4;4;4;4;4;4;4;4]
textdata 8x11 cell
opening the textdata matrix reveals the file, but it is missing the list number. do you know what is causing this?
Guillaume
Guillaume on 24 Dec 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 on 24 Dec 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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags

Products


Release

R2019b