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