memmapfile designed to read binary files, so you are having problems with your text file. There are characters in the data, so you have to read them as characters and then parse them into numbers. More on this below.
The binary contains more than just a stream of floating point values ββwritten in binary format. I see identifiers (lines) and other things in the file. Your only hope of reading is to contact the manufacturer of the device that created the binary file and ask them how to read in such files. There will probably be an SDK, or at least a format description. Perhaps you should take a look at this, since the floating point numbers in your text file may be truncated, i.e. You have lost accuracy compared to reading the binary representation of floats directly.
So how to read a file using memmapfile ? This post contains some tips.
So, first we open the file as 'uint8' (note that there is no 'char' option, so as a workaround we read the contents of the file in the same size):
m = memmapfile('RTL5_57.txt','Format','uint8'); % uint8 is default, you could leave that off
We can display the data read as uint8 as characters, translating it to char:
c = char(m.Data(1:19)).' % read the first three lines. NB: transpose just for getting nice output, don't use it in your code c = 0.398516 0.063440 0.399611 0.063284 0.398985 0.061253
Since each line in your file has the same length (2 * 8 characters for numbers, 1 tab and 2 characters for a new line = 19 characters), we can read N lines from a file by reading N*19 values, So m.Data(1:19) will deliver you the first line, m.Data(20:38) , the second line and m.Data(20:57) second and third lines. Read as much as you want immediately.
Then we have to parse the data to read into floating point numbers:
f = sscanf(c,'%f') f = 0.3985 0.0634 0.3996 0.0633 0.3990 0.0613
All that remains now is to change them to a two-column format
d = reshape(f,2,[]).' d = 0.3985 0.0634 0.3996 0.0633 0.3990 0.0613
Easier than using memmapfile : You do not need to use memmapfile to solve your problem, and I think this complicates the situation. You can just use fopen and then fread :
fid = fopen('RTL5_57.txt'); c = fread(fid,Nlines*19,'*char'); % now sscanf and reshape as above % NB: one can read the values the text file directly with f = fscanf(fid,'%f',Nlines*19). % However, in testing, I have found calling fread followed by sscanf to be faster % which will make a significant difference when reading such large files.
Using this, you can read pairs of Nlines values ββat a time, process them, and call fread again to read the next Nlines . fread remembers where it is in the file (like fscanf ), so just use the same call to get the next lines. Thus, it is easy to write a loop to process the entire file, testing with feof(fid) if you are at the end of the file.
An even simpler method is textscan here : use textscan . To adapt your sample code a bit:
Nlines = 10000; % describe the format of the data % for more information, see the textscan reference page format = '%f\t%f'; fid = fopen('RTL5_57.txt'); while ~feof(fid) C = textscan(fid, format, Nlines, 'CollectOutput', true); d = C{1}; % immediately clear C at this point if you need the memory! % process d end fclose(fid);
Once again, fread followed by sscanf will be faster. Note, however, that the fread method will die as soon as there is one line in the text file that does not exactly match your format. textscan forgives changing spaces on the other hand and therefore more robust.