delimit a text file based on columns of cells

조회 수: 1 (최근 30일)
avram alter
avram alter 2019년 12월 5일
답변: avram alter 2019년 12월 5일
I have a text file that 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 that there is a return before each LOAD, making a total of 8 separated lines. I then have a code which writes in replacements for the (m), (n), and (x) variables:
% Initialize by cleaning up
clear
%add complete paths to every template file you will be using
addpath('C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\1',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\2',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\3',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\4',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\1',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\2',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\3',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\4');
% this bit will take user input, and replace specific characters found in
% the templates, using strrep(str, old, new). In this case, strings being
% replaced are the characters (m), (n), and (x).
user_input_exp = input('Enter Experiment Number: ', 's');
user_input_squad = input('Enter Squad Number: ', 's');
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);
todaysMac = string(todaysMac); % RAMI: THIS IS THE TROUBLESOME LINE
fid = fopen('C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\Completed_Macros\YesterdaysMac.txt','wt');
fprintf(fid, '%s', todaysMac);
fclose(fid);
this works fine, saving to the correct location. the problem is that there is no longer any returns, so my new file looks like this:
LOAD BOX 1 SUBJ M1_299633_D295158_JUN19@1910_Aut_ERROR2 EXPT St8_Se6_Rat1 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 4LOAD BOX 2 SUBJ M2_297928_D294277_APR19@1910_Aut_ERROR2 EXPT St8_Se6_Rat2 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 4LOAD BOX 3 SUBJ M3_299632_D295158_JUN19@1910_Aut_ERROR2 EXPT St8_Se6_Rat3 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 4LOAD BOX 4 SUBJ M4_297929_D294277_APR19@1910_Aut_ERROR2 EXPT St8_Se6_Rat4 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 4LOAD BOX 5 SUBJ F5_299621_D295158_JUN19@1910_Aut_ERROR2 EXPT St8_Se6_Rat5 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 4LOAD BOX 6 SUBJ F6_297923_D294277_APR19@1910_Aut_ERROR2 EXPT St8_Se6_Rat6 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbi 4LOAD BOX 7 SUBJ F7_299626_D295158_JUN19@1910_Aut_ERROR2 EXPT St8_Se6_Rat7 GROUP 1 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 4LOAD BOX 8 SUBJ F8_297924_D294277_APR19@1910_Aut_ERROR2 EXPT St8_Se6_Rat8 GROUP 2 PROGRAM 1908_SIP_EPHYS_ERROR2_St3_TrainWZ_Cbii 4
Which does not work for my use case. How can I get the ouput file to look like the input file (other than the variable changes)?
  댓글 수: 1
avram alter
avram alter 2019년 12월 5일
I guess it can also be delimited by the LOAD BOX string

댓글을 달려면 로그인하십시오.

채택된 답변

avram alter
avram alter 2019년 12월 5일
I figured out how to do it. basically, use the function
newstr = compose(str)
so I just added the newline character to the end of each of my lines, and used that.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by