Importing and Editing a text file using strrep and user inputs

4 views (last 30 days)
I have a text file called EXP1_SQ1_Template.txt, that has 8 lines. it looks like this:
LOAD BOX 1 SUBJ M1_299633_D295158_JUN19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat1 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi (x)
LOAD BOX 2 SUBJ M2_297928_D294277_APR19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat2 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi (x)
LOAD BOX 3 SUBJ M3_299632_D295158_JUN19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat3 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii (x)
LOAD BOX 4 SUBJ M4_297929_D294277_APR19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat4 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii (x)
LOAD BOX 5 SUBJ F5_299621_D295158_JUN19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat5 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi (x)
LOAD BOX 6 SUBJ F6_297923_D294277_APR19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat6 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi (x)
LOAD BOX 7 SUBJ F7_299626_D295158_JUN19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat7 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii (x)
LOAD BOX 8 SUBJ F8_297924_D294277_APR19@1910_Aut_ERROR2 EXPT St(m)_Se(n)_Rat8 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii (x)
note the variables in (). I want a user to edit those, with user input. to that end, I wrote a code that is supposed to import this file, and then replace the strings.
here is the code:
Mac_Templ = importdata('EXP1_SQ1_Template.txt')
user_input_stage = input('Enter correct Stage: ');
Stage_Correction = strrep(Mac_Data, '(m)', user_input_stage);
user_input_session = input('Enter correct Session: ');
Session_Correction = strrep(Mac_Templ, '(n)', user_input_session);
user_input_list = input('Enter correct List: ');
List_Correction = strrep(Mac_Data, '(x)', user_input_list);
nothing is working as the Mac_Templ variable is saving the string "Exp1_SQ1_Template.txt". I am not sure what I am doing wrong. using
Exist('EXP1_SQ1_Template.txt')
returns a 2. thanks for the help

Accepted Answer

avram alter
avram alter on 4 Dec 2019
I found the answer, here is the corrected code:
clear
Mac_Templ = importdata('EXP1_SQ1_Template.txt');
user_input_stage = input('Enter correct Stage: ','s');
todaysMac = Mac_Templ;
todaysMac = strrep(todaysMac, '(m)', user_input_stage);
user_input_session = input('Enter correct Session: ','s');
todaysMac = strrep(todaysMac, '(n)', user_input_session);
user_input_list = input('Enter correct List: ','s');
todaysMac = strrep(todaysMac, '(x)', user_input_list);

More Answers (1)

Walter Roberson
Walter Roberson on 4 Dec 2019
Edited: Walter Roberson on 5 Dec 2019
Stage_Correction = strrep(Mac_Data, '(m)', user_input_stage);
Mac_Data does not exist in your code at that point.
Your replacements are either working with the undefined Mac_Data or with the original Mac_Templ rather than you modifying the string produced by the previous replacement.
user_input_stage = input('Enter correct Stage: ');
The user would have to know to enter quote marks around the text they input. I would suggest that using the 's' option to input() would make sense. Or using inputdlg() to ask all three questions at once.
Have you considered using regexprep() ?
regexprep(Mac_Templ, {'\(m\)', '\(n\)', '\(x\)'}, {user_input_stage, user_input_session, user_input_list})
Note that the ( and ) have to be escaped in this context, as () around a pattern means "grouping" in regexprep.
  1 Comment
avram alter
avram alter on 4 Dec 2019
using 's' is exactly what fixed it, as well as properly importing it using importdata. I've never seen regexprep before, thats why I haven't used it.

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!