2014-09-26 23 views
0

我有一個文本文件TF包括一套以下一種字符串之間的字符串最後一部分:MATLAB函數替換知名人物

"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.T", 
"linStru.twoZoneBuildingStructure.north.vol.Xi[1]", 
"linStru.twoZoneBuildingStructure.south.airLeakage.senTem.T", 
"linStru.twoZoneBuildingStructure.south.vol.Xi[1]", " 
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer1Nf.T[1]", 
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer2Nf.T[2]", 

給定一個線L,從年底開始讓子小號分別表示爲了使其更清楚,對於L = 1:s = T,對於L = 2:s = Xi [1],對於L = 5:s = T [1]等

給定一個以上述格式的文本文件TF,我想寫一個MATLAB函數,它需要TF並替換對應的文件與der(s)在每一行上聯繫。

例如,函數應該改變上述字符串如下:

"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.der(T)", 
"linStru.twoZoneBuildingStructure.north.vol.der(Xi[1])", 
"linStru.twoZoneBuildingStructure.south.airLeakage.senTem.der(T)", 
"linStru.twoZoneBuildingStructure.south.vol.der(Xi[1])", " 
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer1Nf.der(T[1])", 
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer2Nf.der(T[2])", 

怎麼能這樣的功能來寫的?

+0

是TF文本文件的名稱,含電池陣列文件的行,還是什麼? – 2014-09-26 19:47:41

回答

1

我看到在您的文本文件的第四行有一個小的"錯字。我要刪除這個讓事情變得更簡單。

因此,我可以看到你這樣做的最簡單方法是遍歷所有的字符串,刪除單引號,然後找到最後一個.發生的字符串中的點。提取此子字符串,然後在該字符串之間手動插入der()。假設這些字符串位於名爲functions.txt的文本文件中,您可以使用textread讀取文本文件以讀取單個字符串。作爲這樣:

names = textread('functions.txt', '%s'); 

names現在應該名稱的單元陣列,其中每個元素是封裝在雙引號每個字符串。使用findstr來提取.所在的位置,然後提取此位置的最後位置。提取此子字符串,然後用der()替換此字符串。換句話說:

out_strings = cell(1, numel(names)); %// To store output strings 
for idx = 1 : numel(names) 
    %// Extract actual string without quotes and comma 
    name_str = names{idx}(2:end-2); 

    %// Find the last dot 
    dot_locs = findstr(name_str, '.'); 

    %// Last dot location 
    last_dot_loc = dot_locs(end); 

    %// Extract substring after dot 
    last_string = name_str(last_dot_loc+1:end); 

    %// Create new string 
    out_strings{idx} = ['"' name_str(1:last_dot_loc) 'der(' last_string ')",']; 
end 

這是輸出我得到:

celldisp(out_strings) 

out_strings{1} = 

"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.der(T)", 


out_strings{2} = 

"linStru.twoZoneBuildingStructure.north.vol.der(Xi[1])", 


out_strings{3} = 

"linStru.twoZoneBuildingStructure.south.airLeakage.senTem.der(T)", 


out_strings{4} = 

"linStru.twoZoneBuildingStructure.south.vol.der(Xi[1])", 


out_strings{5} = 

"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer1Nf.der(T[1])", 


out_strings{6} = 

"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer2Nf.der(T[2])", 

你想要做的最後一件事是每行文本寫信給你的文本文件。您可以使用fopen打開要寫入的文件。 fopen返回與要寫入的文件關聯的文件ID。然後使用fprintf打印字符串並使用此文件ID爲每個字符串命名一個換行符。然後使用此文件ID使用fclose關閉文件。因此,如果我們想輸出稱爲functions_new.txt一個文本文件,我們會做:

%// Open up the file and get ID 
fid = fopen('functions_new.txt', 'w'); 

%// For each string we have... 
for idx = 1 : numel(out_strings) 
    %// Write the string to file and make a new line 
    fprintf(fid, '%s\n', out_strings{idx}); 
end 

%// Close the file 
fclose(fid); 
+0

完美!我們如何將out_strings寫入另一個名爲functions_new.txt的文本文件? – user3489173 2014-09-26 20:29:40

+0

@ user3489173 - 這很簡單。你可以使用'fopen'和'fprintf'。接下來我會爲你寫一些代碼。 – rayryeng 2014-09-26 20:30:46

+0

@ user3489173 - 完成。祝你好運! – rayryeng 2014-09-26 20:37:17

3

喜歡的東西

regexprep(TF, '\.([^.]+)",$', '.der($1)",', 'dotexceptnewline', 'lineanchors') 

它發現的前一個點之間出現非點的最長的序列和報價 - 逗號 - 結束後,並將其封入der()

+0

+1 - 我無法弄清楚如何用正則表達式來做...在那裏不是很精通。 – rayryeng 2014-09-26 20:00:06

+0

哇我正在努力用正則表達式來找到一系列非點字符的正確語法,謝謝! +1 – 2014-09-26 20:01:11

+0

+1我不知道最後兩個選項'regexprep' – 2014-09-26 20:48:45

0

另一種方式與regexprep做到這一點:

str_out = regexprep(str_in, '\.([^\.]+)"$','\.der($1)"'); 

示例:

str_in = {'"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.T"' 
     '"linStru.twoZoneBuildingStructure.north.vol.Xi[1]"'}; 

這給

str_out = 
    '"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.der(T)"' 
    '"linStru.twoZoneBuildingStructure.north.vol.der(Xi[1])"'