简体   繁体   中英

reading the data of a file into matlab, editing it, and saving it elsewhere

I have a file called EXP1_SQ1_Template.txt. it is a simple text file, containing these 8 lines:

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)

those letters in parentheses are variables I want a user to be able to change.

this is the code (super simple) code I wrote:

Mac_Templ = 'EXP1_SQ1_Template.txt';
Mac_Data = cell2mat(importdata(Mac_Templ));
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);

I have the code run, but the variable 'Mac_Templ' only stores the string 'EXP1_SQ1_Template.txt', and each of the strrep lines only save the number input, not the file itself.

Keeping data in cell format typically makes things more manageable. The below code rebuilds all the command strings based on the user inputs.

Mac_Templ = 'EXP1_SQ1_Template.txt';
Mac_Data = importdata(Mac_Templ);
user_input_stage = input('Enter correct Stage: ');
user_input_session = input('Enter correct Session: ');
user_input_list = input('Enter correct List: ');

for idx=1:size(Mac_Data,1)
    Mac_Data{idx} = strrep(Mac_Data{idx}, '(m)', num2str(user_input_stage));
    Mac_Data{idx} = strrep(Mac_Data{idx}, '(n)', num2str(user_input_session));
    Mac_Data{idx} = strrep(Mac_Data{idx}, '(x)', num2str(user_input_list)); 
end

% Mac_Data now contains the modified command strings
disp('Done')
disp(Mac_Data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM