2010-08-29 64 views
-1

I previously在從C程序(Exe)生成的文本文件中讀取數據時尋求幫助。從MATLAB獲取數據的問題

使用@second的解決方案,我解決了這個問題,但昨天我發現輸出文件比我預期的要複雜。

文件輸出是:

V|0|0|0|t|0|1|1|4|11|T4|H13|||||||||||| 
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0 
*|A1|A1|A7|A16|F|F|F|F|F|F||||||||||||| 
*|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc||||||| 
*|||||kV|kV|kV|MW|MVAR|S|||||||||||| 
N|I|1|N01|N01|132|125.4|138.6|0|0|||||||| 
N|I|1|N02|N02|20|19|21|0|0||||||||||||| 
N|I|1|N03|N03|20|19|21|1.013532234|0.49087611|||||||| 
N|I|1|N04|N04|20|19|21|0.390791617|0.189269056|||||||| 
N|I|1|N05|N05|20|19|21|0.180634542|0.121387171|||||||| 
N|I|1|N06|N06|20|19|21|0.709472564|0.343613323|||||||| 
N|I|1|N07|N07|20|19|21|0.103495727|0.069549543|||||||| 
N|I|1|N08|N08|20|19|21|0.351712456|0.170342158|||||||| 
N|I|1|N09|N09|20|19|21|0.097697904|0.06565339|||||||| 
N|I|1|N10|N10|20|19|21|0.162165157|0.078540184|||||||| 
N|I|1|N11|N11|20|19|21|0|0|||||||| 
*|A1|A8|A7|A7|F|F|F|F||F||F 
*|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2||| 
*|||||km|A|Ohm|Ohm||S||S  
L|I|D10203|N02|N03|1.884|360|0.41071|0.207886957|3.19E-08|3.19E-08||||||||||||| 
L|I|D10304|N03|N04|1.62|360|0.35316|0.1787563|3.19E-08|3.19E-08||||||||||||| 
L|I|D10405|N04|N05|0.532|360|0.11598|0.058702686|3.19E-08|3.19E-08||||||||||||| 
L|I|D10506|N05|N06|1.284|360|0.27991|0.14168092|3.19E-08|3.19E-08||||||||||||| 
L|I|D10607|N06|N07|1.618|280|0.53879|0.194766124|3.00E-08|3.00E-08||||||||||||| 
L|I|D10708|N07|N08|0.532|280|0.17716|0.064039294|3.00E-08|3.00E-08||||||||||||| 
L|I|D10809|N08|N09|2|360|0.436|0.220686791|3.19E-08|3.19E-08||||||||||||| 
L|I|D10910|N09|N10|2.4|360|0.5232|0.264824149|3.19E-08|3.19E-08|||||||||||| 
*|A1|A8|A7|A7|F|F|A1|F|F|F|F|F|F||F||F||||||||||||||||||||||||| 
*|codserv|codice|estr1|estr2|vn1|vn2|nod1|varp|varm|np|Pb|rsd|xsd||bsd1||bsd2|||||||||||| 
*|||||kV|kV||%|%||MVA|%|%||%||%||||| 
%%%%%------%%%%%------%%%% **(read up to here)** 
other unnecessary data 

該算法應:

  • 跳過第一3行
  • 跳過第五行

  • 對於第四行*|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||,保存向量中的每個字符串都是空的codeserv=[] area=[] codice=[] nome=[] tnom=[] tmin=[] tmax=[] pc=[] qc=[] susc=[]

  • 填充向量與以下第四

    codeserv=[N N N N N N N N N N ....] 
    area=[I I I I I I I ....] 
    codice=[1 1 1 1 1 1 ...] 
    nome=[N01 N02 N03 N04 N05 ] 
    tnom=[N01 N02 N03 N04 N05] 
    tmin=[132 20 20.....] 
    tmax=[125.4 19 19 19 ....] 
    pc=[138.6 21 21 21....] 
    qc=[0 0 1.013532234 ....] 
    susc=[0 0 0.49087611] 
    
  • 行中的數據和字符串對數據進行處理以字母L開始一樣。閱讀這條線codice|estr1|estr2|lung|imax|rsd|xsd||bsd1||bsd2並與該行填入值向量與L

    plev=[L L L L L L L ....] 
    area=[I I I I I I I ....] 
    codice=[D10203 D10304 ...] 
    estr1=[N02 N03 N04 N05 ...] 
    estr2=[N03 N04 N05...] 
    lung=[1.884 1,662 ....] 
    imax=[360 360 .....] 
    rsd=[number....] 
    xsd=[number....] 
    bsd1=[number ....] 
    bsd2=[number....] 
    

我試着去適應從以前的問題的代碼開始,但考慮到與NL開始的行不知道有多少我需要知道如何讀取第一個字符串並計數NL是。

read 
[vp***NNNNNNNNNNNNNNNNNNNNNNNNLLLLLLLLLLLLLLLLLLLLL***] 

length N 

length L 

skip 1 2 3 line 

read 4 line, create vector 

codeserv=[N N N N N N N N N N ....] 
area=[I I I I I I I ....] 
codice=[1 1 1 1 1 1 ...] 
nome=[N01 N02 N03 N04 N05 ] 
tnom=[N01 N02 N03 N04 N05] 
tmin=[132 20 20.....] 
tmax=[125.4 19 19 19 ....] 
pc=[138.6 21 21 21....] 
qc=[0 0 1.013532234 ....] 
susc=[0 0 0.49087611] 

skip length N +1 line 

read *|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2||| 

skip length N +3 line 

create 

plev=[L L L L L L L ....] 
area=[I I I I I I I ....] 
codice=[D10203 D10304 ...] 
estr1=[N02 N03 N04 N05 ...] 
estr2=[N03 N04 N05...] 
lung=[1.884 1,662 ....] 
imax=[360 360 .....] 
rsd=[number....] 
xsd=[number....] 
bsd1=[number ....] 
bsd2=[number....] 

close the cycle 

我希望這是可以理解的。我最大的問題是計算文本中的NL

+0

到目前爲止你有什麼? – Woot4Moo 2010-08-29 16:17:10

+0

http://stackoverflow.com/questions/3584003/acquire-data-from-text-files-in-matlab/3584406 僅用於採購的Th代碼N行 – Marietto85 2010-08-29 16:34:20

+0

這是一次性鍛鍊還是您需要對很多類似的文件重複這一點? – second 2010-08-29 18:08:21

回答

1
function readtest2() 

fid = fopen('test2.txt'); 

skipLines(3) 
names1 = getNames; 
skipLines(1); 
nEntries1 = countPrefix('N'); 
data1 = textscan(fid,'%s %s %d %s %s %d %d %f %f %f %[| ]', nEntries1, 'delimiter','|'); 


skipLines(2) 
names2 = getNames; 
skipLines(1); 
nEntries2 = countPrefix('L'); 
data2 = textscan(fid,'%s %s %s %s %s %f %d %f %f %f %f %[| ]', nEntries2, 'delimiter','|'); 

fclose(fid); 

getData(data1, names1); 
getData(data2, names2); 



function names = getNames() 
    names = fgetl(fid); 
    names = textscan(names,'%s','delimiter','|'); 
end 

function getData(data, names) 

    for i = 1:size(data,2)-1 
    values = (data{i}(1:end)); 
    if(iscell(values)) 
     values = cell2mat(values); 
    end 

    name = names{1}{i+1}; 

    % very basic error checking 
    if(~strcmp(name, '')) 

     %save the value in the calling work space 
     assignin('base', name, values) 
    end 
    end 

end 


function skipLines(n) 
    while(n > 0) 
    fgetl(fid); 
    n = n - 1; 
    end 
end 

function n = countPrefix(prefix) 
    pos = ftell(fid); 

    n = 0; 
    currLine = fgetl(fid); 
    while(currLine(1) == prefix) 
    currLine = fgetl(fid); 
    n = n + 1; 
    end 
    fseek(fid, pos, 'bof'); 
end 


end 
+0

感謝您對我的優秀soluzione.Funziona不錯,但很累,瞭解它是如何工作(我知道我是一個糟糕的程序員)原諒延遲我的迴應,但我有一種疾病,我已經檢查了答案。 – Marietto85 2010-09-06 08:46:17