How to concatenate a string in regexprep to include a variable and a string as the regular expression
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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')
n=5
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 Commenti
Stephen23
il 26 Nov 2019
Modificato: Stephen23
il 26 Nov 2019
Perhaps something like this:
>> C = {'','\+i','\+s'};
>> F = @(n)sprintf('%dp%s',fix(n/3),C{1+mod(n,3)});
>> G = @(s)regexprep(F(sscanf(s,'%d')),{'^0p','^\\\+'},'');
>> rgx = sprintf('A%d%s',4,'\((\d+)\)'); % specify the "n" value here
>> rpl = 'A_\{\\mathrm\{${G($1)}\}\}';
>> regexprep('hello A4(1) world',rgx,rpl)
ans =
hello A_{\mathrm{i}} world
>> regexprep('hello A4(2) world',rgx,rpl)
ans =
hello A_{\mathrm{s}} world
>> regexprep('hello A4(3) world',rgx,rpl)
ans =
hello A_{\mathrm{1p}} world
>> regexprep('hello A4(4) world',rgx,rpl)
ans =
hello A_{\mathrm{1p\+i}} world
>> regexprep('hello A4(5) world',rgx,rpl)
ans =
hello A_{\mathrm{1p\+s}} world
>> regexprep('hello A4(6) world',rgx,rpl)
ans =
hello A_{\mathrm{2p}} world
>> regexprep('hello A4(7) world',rgx,rpl)
ans =
hello A_{\mathrm{2p\+i}} world
You might like to download my FEX submission:
it lets you quickly try regular expressions and see regexp's outputs as you type.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!