Editing Specific numbers in a .txt file with matlab GUI Using search/replace
2 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Image Analyst
2019 年 12 月 3 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!