MATLAB: Parsing text file to get rid of specific number and preceding identifier

text file textscan formatspec

I have a text file with lines of the format
AZ 55555.00000 EL 1.23450 Z 55555.24716 X 3.09432 Y 55555.00000 F 55555.00000
AZ 4.00001 EL 55555.00000 Z 5.02117 X 4.39393 Y 7.83839 F 8.39394
How do I write a new text file that gets rid of all numbers beginning with 555 and also gets rid of the preceding identifier, such as AZ? The lines vary, so the 555 string appears with different identifiers throughout the file. Thanks!

Best Answer

fmt='%s %f'; % set format
fid=fopen('lori.dat'); % read data
dat=textscan(fid,fmt,'collect',1); % cellstr and cell array of double
fid=fclose(fid); % done with the file
N=55555; % set the magic number
ix=cell2mat(cellfun(@(x) fix(x)~=N,dat(2),'uniform',0)); % find other locations
t=table(dat{1}(ix),dat{2}(~ix)); % and put those into a table
See what we got--
>> t
t =
7×2 table
Var1 Var2
____ ______
'EL' 1.2345
'X' 3.0943
'AZ' 4
'Z' 5.0212
'X' 4.3939
'Y' 7.8384
'F' 8.3939
>>
Write to file as whatever format desired...
You could also read both columns a strings and do a string search to locate the values if it were really a case where there were differing numbers of 5's in the various locations.