我讀到,在Matlab中,可以在一個regex轉換里面包含一個函式呼叫,就像這樣$1double$2[${doubleTextNumber(4)}],假設1、2、3是一些regex組,4是一個純粹的數字組。我想做的確切事情是捕獲所有由creal_T型別組成的陣列,將該型別替換為double,并將陣列的長度加倍。
codeText = "tymedef struct {
double tolRob;
creal_T Mt2o[704];
creal_T Ho2o[704];
creal_T Ht2t[704];
creal_T Zo2t[704];
creal_T Ztd[64]。
} testType;"
因此,我想讓上面的結構變成:
typedef struct {
double tolRob;
double Mt2o[1408] 。
雙Ho2o[1408]。
雙Ht2t[1408]。
double Zo2t[1408]。
雙重Ztd[128]。
} SpdEstType。
在Matlab中,我做了一個將數字轉換為文本的函式,并將其翻倍:
function [doubleValue] = doubleTextNumber(inputNumber)
doubleValue = string(str2double(inputNumber)*2.0) 。
結束。
我還有一個詞條,我希望它能找到每個宣告中的數字,并將其輸入到函式中:
resultString = regexprep(
codeText,
'(?m)^(W*)creal_T(s*w*)([([^]]*d )])'/span>,
"$1double$2[${doubleTextNumber(4)}]")。)
然而,當我運行這段和平的代碼時,Matlab給了我下面的錯誤味精:
Error using regexprep
對'doubleTextNumber($4)'的評估失敗。
title">輸入 arguments of type 'char' 。
據我所知,我已經讓這個方法做了char的轉換,并希望它也能接受我的regex中的這個值。我已經測驗過了,當我直接輸入'704'或 "704 "時,它是有效的,而且在這個插入的程序中,重合碼也是有效的。
為什么Matlab不能從我的regex中找到這個函式?(它們在同一個m檔案中)
uj5u.com熱心網友回復:
看來我原來的方法有3個問題:
為了讓regexprep()識別我的函式,它必須被移到它自己的m-file中。簡單地從同一檔案中呼叫一個方法是行不通的。
我正在使用https://regex101.com/來編輯搜索運算式,但是即使它似乎正在選擇括號內的數字,在Matlab中,第4組也沒有被regexprep()所填充。一個新的版本確實起作用了,并且用我想要的數字填充了第3組。(?m)^(W*)creal_T(s*w*). ([^]]*d*)]/code>
我還為我的乘法方法添加了更多的轉換選項,以防輸入是數字和char陣列的組合。
我的regex呼叫的最終版本變為:
resultString = regexprep(
codeText。
'(?m)^(W*)creal_T(s*w*). ([^]]*d*)],
"$1double$2[${multiplyTextNumbers(3,2)}]")。)
其中multiplyTextNumbers()在它自己的m檔案中定義為
function [productText] = multiplyTextNumbers(inputFactorText1, inputFactorText2)
%MULTIPLY 這個方法將數字作為輸入,并接受字串,。
%char或double或三者的任何組合。回傳一個帶有的字串。
%resulting product。
if (isstring(inputFactorText1) || ischar(inputFactorText1))
inputFactor1 = str2double(inputFactorText1);
else
inputFactor1 = inputFactorText1;
end
if (isstring(inputFactorText2) || ischar(inputFactorText2))
inputFactor2 = str2double(inputFactorText2);
else
inputFactor2 = inputFactorText2;
end
productText = sprintf('%d', inputFactor1*inputFactor2)。
結束。
希望這能對面臨類似問題的其他人有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326927.html
標籤:
上一篇:C#winformslabel.size不能截斷標簽文本
下一篇:洗掉R中的準空行
