Modify text in a column

조회 수: 1 (최근 30일)
Marco Sette
Marco Sette 2019년 6월 26일
댓글: Walter Roberson 2019년 6월 26일
Dear all, I want to modify just the first column of a file, removing the letters and keeping the numbers. The columns contain string like d200n-h. How it is possible?
Thanks,
Marco

답변 (1개)

Walter Roberson
Walter Roberson 2019년 6월 26일
편집: Walter Roberson 2019년 6월 26일
Assuming comma delimited:
S = fileread('YourFile.txt');
newS = regexprep(S, '^\D*(\d+)[^,]+(,.*$)', '$1$2', 'lineanchors', 'dotexceptnewline');
fid = fopen('YourNewFile.txt');
fwrite(fid, newS);
fclose(fid)
This should work even if there is "" around the string, but it will remove the "" if present.
  댓글 수: 2
Marco Sette
Marco Sette 2019년 6월 26일
Thanks but the third line seems to have some problem
Error: File: filter_first_column.m Line: 3 Column: 13
Invalid expression. Check for missing multiplication operator, missing or unbalanced
delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.
-------
S = fileread('results1bar.txt');
newS = regexprep(S, '^\D*(\d+)[^,]+(,.*$)', '$1$2', 'lineanchors', 'dotexceptnewline');
fid = fopen'test.txt');
fwrite(fid, newS);
fclose(fid)
Walter Roberson
Walter Roberson 2019년 6월 26일
Repaired.

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

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by