How can I process each line or word in a text file using MATLAB?
3 views (last 30 days)
Show older comments
The contents of the text file are in this format, where each line consists of two names with a number attached with an underscore:
Abdel_Madi_Shabneh_0001 Dean_Barker_0001
Abdel_Madi_Shabneh_0001 Giancarlo_Fisichella_0001
Abdel_Madi_Shabneh_0001 Mikhail_Gorbachev_0001
Abdul_Rahman_0001 Portia_de_Rossi_0001
Abel_Pacheco_0001 Jong_Thae_Hwa_0002
Abel_Pacheco_0002 Jean-Francois_Lemounier_0001
and I'd like to reformat the content to make it like this, in other words but them into cell of nX2 dimension so I can each them easily:
'Abdel_Madi_Shabneh_0001' 'Dean_Barker_0001';
'Abdel_Madi_Shabneh_0001' 'Giancarlo_Fisichella_0001';
'Abdel_Madi_Shabneh_0001' 'Mikhail_Gorbachev_0001';
'Abdul_Rahman_0001' 'Portia_de_Rossi_0001';
'Abel_Pacheco_0001' 'Jong_Thae_Hwa_0002';
'Abel_Pacheco_0002' 'Jean-Francois_Lemounier_0001';
Is there a way how can I do it automatically? Thanks
2 Comments
Accepted Answer
Stephen23
on 1 Nov 2015
Edited: Stephen23
on 1 Nov 2015
fid = fopen('temp.txt','rt');
C = textscan(fid,'%s%s');
fclose(fid);
C = cat(2,C{:});
to get this:
>> C
C =
'Abdel_Madi_Shabneh_0001' 'Dean_Barker_0001'
'Abdel_Madi_Shabneh_0001' 'Giancarlo_Fisichella_0001'
'Abdel_Madi_Shabneh_0001' 'Mikhail_Gorbachev_0001'
'Abdul_Rahman_0001' 'Portia_de_Rossi_0001'
'Abel_Pacheco_0001' 'Jong_Thae_Hwa_0002'
'Abel_Pacheco_0002' 'Jean-Francois_Lemounier_0001'
The example text file is attached here:
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!