How can I process each line or word in a text file using MATLAB?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2015-11-1
编辑:Stephen23
2015-11-1
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!