How to concatenate a string in regexprep to include a variable and a string as the regular expression

3 ビュー (過去 30 日間)
I am trying to do a number of things in regexprep. I solve a number of cascading differential equations and then I want to quickly write them in latex. This will undoubtebly get bigger and so good programming is requried, hence why I pass the buck on....
I first want to read in the file and decide which 'order' of differnetial equations I want to computer. In this example I pick order 5 and so it goes that order differential equation in the file and strips the differential eqaution by the square brackets.
Then I want to return A5(1) to be A_{\mathrm{i}} in a text file BUT if it were the fourth order I wanted to do I would want A4(1) to be A_{\mathrm{i}} and so I want to concatenate ['A',nstr,'(1)'] to be A_{\mathrm{i}} where nstr is the nth string of the differential equation order.
I then want to also replace the kps string with k_{\mathrm{p+s}} but I can't get the plus sign to work out at all. I believe this is because plus is some kind of operator even though I stick it in the string. I have tried using '\d+', '\+' and it did not work. I still get some funny business.
Here is my MW (my code) you will need to locate the attached Matlab script.txt file on your own PC. help is much appreciated.
input = fileread('........\Matlab script.txt')
nstr = num2str(n)
input_str = extractBetween(input,['@(x,A',nstr',')['],']');
Multiplication_delete = regexprep(input_str,'*','');
ConjAmpswap = strrep(Multiplication_delete,{['conj(A',nstr,'(1))'],['conj(A',nstr,'(2))'],['conj(A',nstr,'(3))'],['conj(A',nstr,'(4))'],['conj(A',nstr,'(5))'],['conj(A',nstr,'(6))'],['conj(A',nstr,'(7))'],['conj(A',nstr,'(8))'],['conj(A',nstr,'(9))'],['conj(A',nstr,'(10))'],['conj(A',nstr,'(11))'],['conj(A',nstr,'(12))'],['conj(A',nstr,'(13))'],['conj(A',nstr,'(14))'],['conj(A',nstr,'(15))']},{'A_{\\mathrm{i}}^*','A_{\\mathrm{s}}^*','A_{\\mathrm{p}}^*','A_{\\mathrm{p\+i}}^*','A_{\\mathrm{p\+s}}^*','A_{\\mathrm{2p}}^*','A_{\\mathrm{2p\+i}}^*','A_{\\mathrm{2p\+s}}^*','A_{\\mathrm{3p}}^*','A_{\\mathrm{3p\+i}}^*','A_{\\mathrm{3p\+s}}^*','A_{\\mathrm{4p}}^*','A_{\\mathrm{4p\+i}}^*','A_{\\mathrm{4p\+s}}^*','A_{\\mathrm{5p}}^*'});
Ampswap = regexprep(ConjAmpswap,{['A',nstr,'(1)'],['A',nstr,'(2)'],['A',nstr,'(3)'],['A',nstr,'(4)'],['A',nstr,'(5)'],['A',nstr,'(6)'],['A',nstr,'(7)'],['A',nstr,'(8)'],['A',nstr,'(9)'],['A',nstr,'(10)'],['A',nstr,'(11)'],['A',nstr,'(12)'],['A',nstr,'(13)'],['A',nstr,'(14)'],['A',nstr,'(15)']},{'A_{\\mathrm{i}}','A_{\\mathrm{s}}','A_{\\mathrm{p}}','A_{\\mathrm{p\+i}}','A_{\\mathrm{p\+s}}','A_{\\mathrm{2p}}','A_{\\mathrm{2p\+i}}','A_{\\mathrm{2p\+s}}','A_{\\mathrm{3p}}','A_{\\mathrm{3p\+i}}','A_{\\mathrm{3p\+s}}','A_{\\mathrm{4p}}','A_{\\mathrm{4p\+i}}','A_{\\mathrm{4p\+s}}','A_{\\mathrm{5p}}'});
kswap = regexprep(Ampswap,{'ki','ks','kp','kpi','kps','k2p','k2pi','k2ps','k3p','k3pi','k3ps','k4p','k4pi','k4ps','k5p'},{'k_{\\mathrm{i}}','k_{\\mathrm{s}}','k_{\\mathrm{p}}','k_{\\mathrm{p\+i}}','k_{\\mathrm{p\+s}}','k_{\\mathrm{2p}}','k_{\\mathrm{2p\+i}}','k_{\\mathrm{2p\+s}}','k_{\\mathrm{3p}}','k_{\\mathrm{3p\+i}}','k_{\\mathrm{3p\+s}}','k_{\\mathrm{4p}}','k_{\\mathrm{4p\+i}}','k_{\\mathrm{4p\+s}}','k_{\\mathrm{5p}}'});
exponentopen = regexprep(kswap,{'exp('},{'e^{'});
exponentclose = regexprep(exponentopen,{'x)'},{'x}'});
beta_factor = regexprep(exponentclose,{'(maxBeta/2)'},{'\\Big(\\dfrac{\\beta}{2}\\Big)'});
i_replace = regexprep(beta_factor,{'1i'},{'i'});
  10 件のコメント
Thomas Dixon
Thomas Dixon 2019 年 11 月 26 日
HAHAHA. This is exactly the reason that you're in the A side of Q&A's. This is a ridculusly elegant solution compared to the hash I was making myself.
Well played sir!


回答 (0 件)


Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by