URGENT: Need to remove negative values from a text file from a certain column

1 view (last 30 days)
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)

Accepted Answer

Sean de Wolski
Sean de Wolski on 29 Jul 2015
Edited: Sean de Wolski on 29 Jul 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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!