Editing Specific numbers in a .txt file with matlab GUI Using search/replace

1 view (last 30 days)
This is copy-pasted from a stack exchange question.
I have a bunch of different template .txt files that I want to be accessed by a user in matlab. these templates look like this, but with key differences that I will explain:
LOAD BOX 1 SUBJ M1_299633_D295158_JUN19@1910_Aut_ERROR2 EXPT St(n)_Se(m)_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(n)_Se(m)_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(n)_Se(m)_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(n)_Se(m)_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(n)_Se(m)_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(n)_Se(m)_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(n)_Se(m)_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(n)_Se(m)_Rat8 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii (x)
Each template will have a different SUBJ name, but they all have this basic format. there is a separate template for each experiment and squad. Each experiment will have its own folder, and within each of those will be a folder for each squad. I have helpfully named them accordingly. Here is the path to experiment 2, squad 3:
C:\Users\Administrator\Experimental_Templates\Experiment_No2\EXP2_SQ3\EXP2_SQ3_Template.txt
within a GUI in matlab, I want a user to select an experiment from a drop down list, and than enter the squad number. This will load the correct template into matlab. I have created a GUI already, and added here both the .fig and .m files. The .m file is empty, other than initialization for the ui elements. the files can be found on my github: https://github.com/Rambunctiouslad/Rat_Tracking.
After the correct Template has been loaded, I want to basically do a find and replace. each of those strings ((n), (m), and (x)) will be replaced with input typed by a user into one of the GUI's editable text boxes. (n) is the stage number, so typing 4 into the stage box will replace the (n) with a 4, and so on.
After this is done, I want the original template to remain the same, with the placeholder variables. However, I want the updated file to be saved into a specific folder, in a .mac format. .mac is not for apple computers, it is a proprietary format used by the med-pc program.

Accepted Answer

Image Analyst
Image Analyst on 3 Dec 2019
Why not simply list all the files in a listbox and let the user click on the file(s) s/he want to process?
Anyway, if you want to prepopulate drop down lists (pop ups) with various numbers or strings, you can get the selected strings from those popups, and build up a filename with sprintf():
index = handles.popExp.Value;
str1 = handles.popExp.String;
experiment = str1{index}
index = handles.popSquad.Value;
str2 = handles.popSquad.String;
squad = str2{index};
folder = sprintf('C:\Users\Administrator\Experimental_Templates\Experiment_No%s\EXP%s_SQ%s', ...
expeiment, experiment, squad)
baseFileName = sprintf('EXP%s_SQ%s_Template.txt', experiment, squad)
fullFileName = fullfile(folder, baseFileName)
Of course, replace the names of the GUI controls with whatever you've chosen.
  1 Comment
avram alter
avram alter on 3 Dec 2019
Edited: avram alter on 3 Dec 2019
good question, I have 2 answers as to why I did things like this.
1) We can be running more than 10 experiments daily, with up to 5 squads for each. a drop down list of 50 different files may not make for the best ux design
2) people act silly sometimes, and having a list of 50 + files to choose from can intimidate people a bit, and they could potentially pick the wrong one.
Here is my adaptation of your code, in the main output function:
function varargout = Macro_Updating_OutputFcn(hObject, eventdata, handles)
index = handles.popupmenu3.Value;
str1 = handles. popupmenu3.String;
popupmenu3 = str1{index}
index = handles.edit9.Value;
str2 = handles.edit9.String;
edit9 = str2{index};
folder = sprintf('C:\Users\Administrator\Experimental_Templates\Experiment_No%s\EXP%s_SQ%s', ...
popupmenu3, popupmenu3, edit9)
baseFileName = sprintf('EXP%s_SQ%s_Template.txt', popupmenu3, edit9)
fullFileName = fullfile(folder, baseFileName)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
I get this error:
popupmenu3 =
'---------------'
Brace indexing is not supported for variables of this type.
Error in Macro_Updating>Macro_Updating_OutputFcn (line 72)
edit9 = str2{index};
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in Macro_Updating (line 42)
gui_mainfcn(gui_State, varargin{:});
I changed the bracing to parentheses, and got this error:
popupmenu3 =
1×1 cell array
{'---------------'}
Array indices must be positive integers or logical values.
Error in Macro_Updating>Macro_Updating_OutputFcn (line 72)
edit9 = str2(index);
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in Macro_Updating (line 42)
gui_mainfcn(gui_State, varargin{:});
I'm sure I am doing something wrong, but I am not sure what.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!