2016-05-31 8 views
0

Ich habe eine entwickelte eine Funktion in Matlab verwenden, die mir erlaubt (oder so dachte ich) Daten aus einer Text-Datei zu extrahieren, die (zumindest der Anfang)Wrong float Präzision textscan in Matlab

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 

wie folgt aussieht die Idee ist es, den Brief, den ich mit seiner Position mit einem Vektor (weil nur die Werte neben M sind wirklich interessant für mich), und erhalten die beiden anderen Zahlen in einem Vektor müssen übereinstimmen. Das Ende der Behandlung funktioniert gut, aber die Werte, die ich bis zum Ende meines Codes bekommen sind manchmal weit von den realen.

Zum Beispiel anstelle von [0 0,013 0,1 0,236 0,425 0,666 0,958] ich [0 0,013 0,1010 0,237 0,426 0,666 0,959]. Das ist nicht so ein Problem, das Problem ist viel schlimmer für die erste Spalte: Statt ein Maximum bei 119, ist es nicht 90 erreicht hatte ich einen Code, der richtig mit ganzen Zahlen gearbeitet, aber jetzt ist es ich bin mit Schwimmern scheitert jedes Mal.

Ich werde versuchen, nur die interessanten Teile des Codes angezeigt werden:

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 => 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 

Die Art der Präzision Ich mag würde, ist haben, Werte zu halten, wie [M108.55 23,759] mit bis zu 3 Ziffern.

Vielen Dank im Voraus, können Sie für jede Information zu fragen, ob ich nur den Teil des Codes zu geben, scheiterte, die das Problem enthält.

+0

Versuchen Sie, nur '% f' als Formatbezeichner verwenden, anstatt'% .5f ' – Suever

+0

Vielen Dank für Ihre Antwort. Wenn gehalten% .5f statt% f als gescheiterter Versuch, etwas zu ändern, aber ich habe die gleichen falschen Werte, wie ich vorher gesagt, es sei denn, eine Stelle mehr Präzision (a 0) –

Antwort

0

Modifizieren eines Bit-Code als:

nom_essai='test.txt';  
fid1 = fopen(nom_essai, 'rt'); 
tableau = textscan(fid1, '%s %f ', 'HeaderLines', 1, 'CollectOutput', true); % Change to %f not to miss significative figures 
colonne_force = tableau{1}; %on recupere la premiere colonne 
colonne_deplacement=tableau{2}; %on recupere la seconde colonne 

% Check if has M 

hasM = cellfun(@(x) any(x == 'M'), colonne_force); 

column2 = colonne_deplacement(hasM); 

column1 = colonne_force(hasM); 
column1 = cellfun(@(x) str2double(x(2:end)), column1); % delete M and convert to double 

Die Genauigkeit beibehalten wird:

Values of vectors

+0

Vielen Dank für diese Antwort, es Ich habe einige Zeit gebraucht, um den Rest meines Codes anzupassen, weil ich alle meine Vektoren (Zeit, Belastung, Stress) gleichzeitig gebaut habe. Ich habe das geändert, weil der Aufbau meines Zeitvektors immer noch eine "for" -Schleife erfordert und nun alles perfekt funktioniert. –