Can regexprep be used for partial text replacements containing special characters?

조회 수: 1 (최근 30일)
I have a text file containing the following 3 lines of data:
"028 2016 10:11:12.12345" 39.028469 -104.484128 2070
"028 2016 10:11:13.1234" 39.028404 -104.484111 2075
"028 2016 10:11:14" 39.028390 -104.484000 2080
I am attempting to use regexprep to replace the time stamp portion (in quotations) with a single numerical value. If successful, the 3 lines of data would look like this:
1 39.028469 -104.484128 2070
2 39.028404 -104.484111 2075
3 39.028390 -104.484000 2080
I'm looking for a reference in the MATLAB documentation that would indicate how to account for the quotation marks, as well as all characters between them. This is my first attempt at using regexprep and I'm not even sure this is possible.
Is there a way to utilize regexprep for this instance?
Thank you.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 3월 25일
편집: Azzi Abdelmalek 2016년 3월 25일
v=importdata('file.txt')
a=regexprep(v,'\<".+"\>','')
b=regexp(a,'\S+','match')
n=numel(b);
out=[(1:n)' str2double(reshape([b{:}],n,[]))]
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2016년 3월 25일
Another alternative is
v=importdata('fic.txt')
w=num2cell(1:numel(v))'
a=cellfun(@(x,y) regexprep(x,'\<".+"\>',num2str(y)),v,w,'un',0)

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by