Wrong float precision using textscan in Matlab
<p>I have a devised a function in matlab that allows me (or so I thought) to extract data from a textfile that looks like this (at least the beginning)</p>

<pre><code>G1 50
G2 50
M-0.35 0
M-0.05 0.013
M3.3 0.1
M9.75 0.236
M17.15 0.425
M25.85 0.666
M35.35 0.958
</code></pre>

<p>The idea is to match the letter I have with its position with a vector (because only the values next to M are really interesting to me), and get the two other numbers in a vector.
The end of the treatment works well, but the values I get by the end of my code are sometimes far from the real ones.</p>

<p>For instance, instead of [0 0.013 0.1 0.236 0.425 0.666 0.958] I get [0 0.013 0.1010 0.237 0.426 0.666 0.959].
This is not such an issue, the problem is much worse for the first column : instead of a maximum at 119, it doesn't reach 90. I had a code that worked properly with integers, but now I'm using floats it fails everytime.</p>

<p>I will try and display only the interesting parts of the code :</p>

<pre><code>nom_essai='test.txt'     
fid1 = fopen(nom_essai, 'rt');
tableau = textscan(fid1, '%s %.5f ', 'HeaderLines', 1, 'CollectOutput', true); %There are a few lines that I skip because they give the parameters, I get them with another line of the code
colonne_force=tableau{1}; %on recupere la premiere colonne
colonne_deplacement=tableau{2}; %on recupere la seconde colonne


indice=2*found_G+found_F+3*found_R; %this is the result of the treatment on colonne_force to match an index with the letter, which helps me keep the period next to G and the 2 values next to M.

force=linspace(0,0,length(n_indices)); %initialisation
    deplacement=linspace(0,0,length(n_indices)); %initialisation
    temps=linspace(0,0,length(n_indices)); %initialisation


    for k=1:length(colonne_force) %%%%k is for the length of my vectors, while j is for the length of the columns
        if indice(k)==2 %un G est trouve =&gt; temps d'echantillonnage
            T=colonne_deplacement(k); %to keep the period next to G
            end
        elseif indice(k)==1 %an F is found : skip it
        elseif indice(k)==3 %an R is found : skip it
        else %an M is found : I need to get the values on these lines
            j=j+1;
            deplacement(j)=colonne_deplacement(k); %I keep the value on the second column
            M=strsplit(colonne_force{k},'M'); %I get the string 'MXXX'
            force(j)=str2double(M{2}); %I recover this string without the M, and convert the number to double                 
        end
    end
</code></pre>

<p>The kind of precision I would like to have is to keep values like [M108.55 23.759] with up to 3 digits.</p>

<p>Thank you in advance, feel free to ask for any information if I failed to give only the part of the code that contains the problem.</p>