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

조회 수: 1 (최근 30일)
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)

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 7월 29일
편집: Sean de Wolski 2015년 7월 29일
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개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by