Change string of text into multiple lines

I have a string of text that needs to be formatted and saved as a txt file. The string is in this format:
,1,1880802442166,1,751702411341,1,751702411358,1,751702411365,1,751702411372,1,751702411389,1,751702459060,1,751702459077,1,751702459084,3,751702459091,1,751702459107,1,751702459114,2,751702459121,1,751702459145...
It needs to be output in this format:
,1,1880802442166
,1,751702411341
,1,751702411358
,1,751702411365
,1,751702411372
,1,751702411389
,1,751702459060
,1,751702459077
,1,751702459084
,3,751702459091
,1,751702459107
,1,751702459114
,2,751702459121
,1,751702459145...
It's for an inventory list that was recorded in the wrong format.
Thanks in advance,
Nick

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 20 日

1 投票

s=',1,1880802442166,1,751702411341,1,751702411358,1,751702411365,1,751702411372,1,751702411389,1,751702459060,1,751702459077,1,751702459084,3,751702459091,1,751702459107,1,751702459114,2,751702459121,1,751702459145'
a=regexp(s,',','split')
a(1)=[]
b=reshape(a,2,[])'
c=cellfun(@(x,y) [',' x ',' y], b(:,1),b(:,2),'un',0)
fid=fopen('file.txt','w')
for k=1:numel(c)
fprintf(fid,'%s \r\n',c{k,:})
end
fclose(fid)

1 件のコメント

Nicholas
Nicholas 2014 年 12 月 20 日
I needed a blank line between each in value in the list, but I just added an extra
\r\n
in
fprintf(fid,'%s \r\n',c{k,:})
Thanks

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 12 月 20 日
編集済み: Andrei Bobrov 2014 年 12 月 20 日

1 投票

s = ',1,1880802442166,1,751702411341,1,751702411358,1,751702411365,1,751702411372,1,751702411389,1,751702459060,1,751702459077,1,751702459084,3,751702459091,1,751702459107,1,751702459114,2,751702459121,1,751702459145';
out = mat2cell(s,1,diff([regexp(s,',\d,'),numel(s)+1]))

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2014 年 12 月 20 日

コメント済み:

2014 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by