How to find strings in a file using MATLAB -
i have file contains repeating strings. file large give simple example:
a b c w g b v f
i want extract a b
array. how can in matlab?
try using textscan. can split file '\n' , whitespace cell2mat.
fid = fopen('your_string_file.ext'); input = textscan(fid, '%s', 'delimiter', '\n'); cellmatrix = cell2mat(input{1}); cellmatrix = b c d f b v f
then if there specific pattern want can walk cellmatrix. assuming want a b
pattern within single row following:
pattern = ['a', 'b']; patindex = 1; dims = size(cellmatrix); i=1:dims(1) patindex = 1; j=1:dims(2) if strcmp(cellmatrix(i,j), ' ') continue end if strcmp(cellmatrix(i,j), pattern(patindex)) patindex = patindex+1; if patindex > length(pattern) found... store location/do want patindex = 1; end else patindex = 1; end end end
you can change check find whatever pattern want matrix.
this assumes file fit memory -- if it's large fit in half memory you'll need trickier incremental passes , file writing.
Comments
Post a Comment