Effacer les filtres
Effacer les filtres

Character restriction in text file

2 vues (au cours des 30 derniers jours)
jgillis16
jgillis16 le 10 Août 2015
Modifié(e) : Cedric le 10 Août 2015
I am trying to restrict a text file by only keeping lines that contain 'M' in the first column.
I wrote the code below, except 'M' cannot be used in str2double. Would there be an alternative way to write this? Code below:
clear all
fidi = fopen('BTM.txt','rt');
Glxc = textscan(fidi, '%s', 'Delimiter','|');
frewind(fidi)
Glxcs = textscan(fidi, '%s', 'EndOfLine','\r\n');
fclose(fidi);
dlen = 2*fix(length(Glxc{:})/2); % Set Row Length
Glxcr = reshape(Glxc{:}(1:dlen), 2, [])'; % Reshape & Transpose
LIdx= (str2double(Glxcr(:,2))<=1813.1) & (str2double(Glxcr(:,1))='M')% Reshape & Transpose
NewGlxc = Glxcs{:}(LIdx,:); % Rows Of New Array
fido = fopen('vcc18M.txt','wt')
fprintf(fido, '%s\n', NewGlxc{:});
fclose(fido)
  2 commentaires
Star Strider
Star Strider le 10 Août 2015
‘... pulling lines that contain 'M' in the first column ...’
Does that mean that you want to keep those lines or discard them?
jgillis16
jgillis16 le 10 Août 2015
keep those lines, discard the rest that don't have 'M' in the first column.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Août 2015
LIdx = (str2double(Glxcr(:,2))<=1813.1) & ~cellfun(@isempty, regexp(Glxcr(:,1),'^M$'))
If the column only needs to start with 'M' but can contain other characters, use '^M' . If the column only needs to have 'M' somewhere in it, use 'M'

Plus de réponses (1)

Cedric
Cedric le 10 Août 2015
Modifié(e) : Cedric le 10 Août 2015
If you want to keep only the lines which start with the character M, the following would work:
content = fileread( 'BTM.txt' ) ;
content = regexprep( content, '^[^M].*?$\s*', '', 'lineanchors' ) ;
And then you export keeping the original format:
fId = fopen( 'vcc18M.txt', 'w' ) ;
fwrite( fId, content ) ;
fclose( fId ) ;
PS: Will you ultimately want to separate lines like here? Were you able to make this one work by the way?
EDIT : Without knowing more about the content of the file, it is difficult to debug what you wrote, but if Glxcr(:,1) is truly indexing cells which contain elements of the first column of the file, then you probably want to replace
& (str2double(Glxcr(:,1))='M')
with
& strcmp( Glxcr(:,1), 'M' )

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by