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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!