MATLAB: Import textfile with both numbers and characters into array while skipping part of the file’s content

fscanftext file

Dear all,
I want to import the following textfile (ongoing) as an array:
expl.PNG
The result should be an array including all values except the content between 5002 and 6002 without including both numbers.
Result:
1
2
40
280
1465
1471
[…]
Thanks, Sarah

Best Answer

With the above analysis of the file structure and ignoring the remainder of the file as there are also no delimiters other than blanks to count to get a number of fields,
fid=fopen('expl_file.txt','r'); % open file
s=string(split(fgetl(fid))); % read the first record & convert to words
fid=fclose(fid); % done with the file
i1=find(s=="5002"); % find the offending 5002
i2=find(s=="6002"); % and its partner...
s(i1(1):i2(2))=[]; % eliminate them and everything in between
v=cellfun(@str2double,s); % convert remainder to numeric
So, what did we get?
>> whos v
Name Size Bytes Class Attributes
v 119996x1 959968 double
>> v(1:20)
ans =
1
2
40
280
1242
1236
1231
1226
1221
1215
1210
1205
1199
1194
1189
1184
1178
1173
1168
1163
>>
I would imagine if one knew the definitions of the remainder of the encodings one could parse the file to actually compute the number of elements in that first record...but that wasn't revealed so have to just parse what one finds.
BTW, the data in the attached file are not those of the original image--I double-checked and the above data are those in the file at the equivalent locations.
ADDENDUM One guesses this is pretty brittle as there may well be no guarantee that either the magic numbers of 5002 and 6002 will be the pertinent values now and forever nor that there couldn't be such a value in the actual data stream itself.
We don't have enough info to know for sure what would be "the better way"