Removing Rows From a Matrix by Label Quickly
이전 댓글 표시
I currently have some code that is destined to: import data as a matrix from a text file, read the data and delete any rows in the matrix that do not start with "H", delete the H label from the remaining rows, print this matrix to a text file. My code is as follows:
FID = fopen('Test_Data_2.txt','rt');
m=[];
while ~feof(FID)
l=fgetl(FID);
if~isempty(l)
if l(1:1)=='H'
m = [m;str2num(l(2:end))];
end
end
end
FID = fclose(FID);
dlmwrite('md_msd.out', m, 'delimiter', '\t', ...
'precision', 6)
This code works great for small data sets but I am going to have to use it for sets of 100000 rows or more. I need a way to speed up the process as my current code takes far too long. Is there any way I can make this code faster?
댓글 수: 5
Matt Kindig
2013년 6월 26일
Can you post a few lines of your txt file, along with your desired output? In particular, it is not really clear which lines you wish to reject.
Matt Kindig
2013년 6월 26일
Walter's suggestion of sed and perl are excellent ones, but for your reference, a pure Matlab approach could involve:
%read full file into memory
% if insufficient memory available, you might need to read into
% batches of bytes at a time, using fread() or similar.
str=fileread('Test_Data_2.txt');
%extract lines starting with 'H'
out = regexp(str, '^\s*H([^\n]+)$', 'match', 'lineAnchors');
%extract decimal numbers from that
m = textscan(cell2mat(out), 'H %f %f %f', 'CollectOutput', true);
m = m{1}; %this is your data matrix
%write it to a text file
dlmwrite('md_msd.out', m, 'delimiter', '\t', ...
'precision', 6);
Walter Roberson
2013년 6월 27일
cell2mat() could fail there because the lines might not be exactly the same length. You could cellstr() to ensure the shorter lines are padded with blanks.
However, textscan() cannot accept arrays of char as the first argument (or cell array of char either.) You would have to reconstruct as a single string with \n between the lines in order to use textscan in this manner.
textscan(sprintf('%s\n', out{:}), .....)
Joseph
2013년 7월 2일
답변 (1개)
Walter Roberson
2013년 6월 26일
Use other tools when it makes sense to do so. For example, in Linux or OS-X from their shells:
sed -e '/^$|^[^H]/d', -e 's/^H//p' < Test_Data_2.txt > md_msd.out
Or perl
perl -e '/^H/ && s/^H// && print' < Test_Data_2.txt > md_msd.out
You can invoke perl from within MATLAB using the perl() command.
댓글 수: 3
Joseph
2013년 6월 26일
Walter Roberson
2013년 6월 27일
perl is part of the MATLAB distribution on MS Windows, so there is no need to install it. On OS-X and Linux, I believe those are defined to include perl as part of the operating system, so there is no need to install perl on those systems either.
Joseph
2013년 6월 29일
카테고리
도움말 센터 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!