Modify text in a column

2 vues (au cours des 30 derniers jours)
Marco Sette
Marco Sette le 26 Juin 2019
Commenté : Walter Roberson le 26 Juin 2019
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

Réponses (1)

Walter Roberson
Walter Roberson le 26 Juin 2019
Modifié(e) : Walter Roberson le 26 Juin 2019
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 commentaires
Marco Sette
Marco Sette le 26 Juin 2019
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 le 26 Juin 2019
Repaired.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by