2016-04-10 14 views
0

Wie kann ich mit einer Textdatei, wie unten gezeigt, die Inhalte mit MATLAB extrahieren? Die Anzahl der Klammern entspricht #Pins (Spalte 3). Ich benutzte textscan und regexp aber war erfolglos.Inhalte von mehreren Klammern in Matlab extrahieren?

Net Name  #Pins Driver  Recvs 

0 o_6_  2 ( 1, 13) ( 0, 0) 

1 i_9_  5 ( 6, 1) ( 2, 0) ( 21, 1) ( 28, 2) ( 38, 3) 

2 n_n22  4 ( 25, 13) ( 2, 1) ( 3, 7) ( 22, 0) 

3 [286]  2 ( 8, 13) ( 1, 2) 
+1

Was ist die erwartete Ausgabe? – rayryeng

+0

@rayryeng für jede Zeile, Zahlen in Klammern (in einer Array-Struktur) zum Beispiel für netto 0, [1,13] und [0,0] –

Antwort

0

können Sie fgetl verwenden Zeile für Zeile der Eingabedatei in einer Schleife zu lesen.

Dann können Sie strtok verwenden, um die aktuelle Zeile mit ( als delimiter innerhalb einer Schleife zu analysieren.

Der nächste Schritt besteht darin, in jedem Token , und ) zu ersetzen und die resultierende Zeichenfolge in ein Array mit str2num zu konvertieren.

In der Frage, die Sie nicht angeben, wie die Daten extrahiert zu speichern, so dass zwei Möglichkeiten sein könnte:

  • speichern die Daten in einer Struktur wird jedes Feld entsprechend der ID des „Net“ genannt (die Namen des Feldes können created dinamically
  • speichern die Daten in ein cellarray

sein Dieser Ansatz wird im folgenden Code iimplemented wurde;. die Kommentare im Code sollte die verschiedenen Schritte erklären

% Open the input file 
fp_in=fopen('read_bra.txt','rt'); 
% Initiaize the output cellarray 
data_cell{1}=[]; 
% Initialize the counter for the cellarray 
cnt=0; 
% Loop to read the input file, line by line 
while 1 
    % Read a line 
    tline = fgetl(fp_in); 
    % If the last line of the file has been read 
    if(~ischar(tline)) 
     % Close the input file and exit from the (infinite) loop 
     fclose(fp_in); 
     break 
    end 
    % split current line into tokens 
    [token, remain]=strtok(tline,'('); 
    % The "once" flag: 
    % once == 1: read the ID of the "Net" 
    %    reset the temporary matrix "v" 
    %    Initialize the output sctructure field 
    %    Increment the counter for the cellarray 
    % once == 2: current line is valid (is not empty, and contains data 
    %    store the data in the output struct 
    %    store the data in the output cellarray 
    once=1; 
    while(~isempty(remain)) 
     if(once == 1) 
     net_n=sscanf(tline,'%d'); 
     once=2; 
     v=[]; 
     data_str.(['net_' num2str(net_n)])=[]; 
     cnt=cnt+1; 
     end 
     % Split the line wrt "(" 
     [token, remain]=strtok(remain,'('); 
     % Store the data from the current token in the temporary matrix 
     v=[v;str2num(strrep(strrep(token,',',''),')',''))]; 
    end 
    % Once all the data of the current line have been extracted 
    if(once == 2) 
     % Store the data in teh output struct 
     data_str.(['net_' num2str(net_n)])=v; 
     % Store the data in teh output cellarray 
     data_cell{cnt}=v; 
     % Resert the "once" flag 
     once=0; 
    end 
end 

die Eingabedatei, die Sie geschrieben haben Gegeben ist die Ausgabe:

data_str = 

    net_0: [2x2 double] 
    net_1: [5x2 double] 
    net_2: [4x2 double] 
    net_3: [2x2 double] 


data_str.net_0 
ans = 
    1 13 
    0  0 
data_str.net_1 
ans = 
    6  1 
    2  0 
    21  1 
    28  2 
    38  3 
data_str.net_2 
ans = 
    25 13 
    2  1 
    3  7 
    22  0 
data_str.net_3 
ans = 
    8 13 
    1  2 


data_cell = 

    [2x2 double] [5x2 double] [4x2 double] [2x2 double] 

data_cell{1} 
ans = 
    1 13 
    0  0 
data_cell{2} 
ans = 
    6  1 
    2  0 
    21  1 
    28  2 
    38  3 
data_cell{3} 
ans = 
    25 13 
    2  1 
    3  7 
    22  0 
data_cell{4} 
ans = 
    8 13 
    1  2 

Hoffnung, das hilft.

Qapla '

+0

ist es fantastisch, danke. –

Verwandte Themen