URGENT: Need to remove negative values from a text file from a certain column
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
jgillis16
il 29 Lug 2015
Commentato: Walter Roberson
il 29 Lug 2015
I need to remove all negative values within column 5, and keep only values between 0 and 7 in column 5 in the text file.
I drafted the code below, but for some reason negative values keep slipping into my data set. My files are attached as well.
clear all
fidi = fopen('faintgrm.txt','rt');
Glxc = textscan(fidi, '%s', 'Delimiter','|');
frewind(fidi)
Glxcs = textscan(fidi, '%s', 'EndOfLine','\r\n');
fclose(fidi);
dlen = 18*fix(length(Glxc{:})/18); % Set Row Length
Glxcr = reshape(Glxc{:}(1:dlen), 18, [])'; % Reshape & Transpose
%Idx = cellfun(@(x) str2num(x) <= 10, Glxcr(:,5), 'Uni',0);
Idx = cellfun(@(x) str2num(x) >= 0, Glxcr(:,5), 'Uni',0); % Find Rows With Col15 < 21
LIdx = logical(cell2mat(Idx)); % Logical Array From Cell
NewGlxc = Glxcs{:}(LIdx,:); % Rows Of New Array
fido = fopen('spiralsfaintrm.txt','wt')
fprintf(fido, '%s\n', NewGlxc{:});
fclose(fido)
Risposta accettata
Sean de Wolski
il 29 Lug 2015
Modificato: Sean de Wolski
il 29 Lug 2015
Open the file with the import tool (right click on it, import data), select | as the delimiter, import it. From there, extract fifth column and do what you want:
col5 = cell2mat(faintgrm(:,5));
idx = col5 < 0; % these values less than 0
Decide what you want to do and then write it out with textscan as you're doing above.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!