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

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

"I then want to also replace the kps string with k_{\mathrm{p+s}} but I can't get the plus sign to work..."
>> str = 'hello kps world';
>> regexprep(str,'kps','k_\{\\mathrm\{p\+s\}\}')
ans =
hello k_{\mathrm{p+s}} world
strrep would probably be easier to use, as you can avoid the significant overlap between LateX and regular expression active characters (which need escaping):
>> strrep(str,'kps','k_{\mathrm{p+s}}')
ans =
hello k_{\mathrm{p+s}} world
Thank you for rapid response Stephen. Do you know if this works from mutliple examples. I just tried it and it didnt like the fact I had 15 strreps to do. The example you give gives a nice working solution but for example:
str = 'hello kps kpi world';
strrep(str,{'kps','kpi'},{'k_{\mathrm{p+s}}','oh + bugger'})
gives me a string of one or the other being chnaged byt not both simultanously.
ans =
1×2 cell array
{'hello k_{\mathrm{p+s}} kpi world'} {'hello kps oh + bugger world'}
@Thomas Dixon: you would need to call strrep in a loop or cellfun.
You might also be able to find a function to help you:
Note that things like replacing exp(...) with e^{...} could be done in one step with regexprep and dynamic regular expressions (as long as there is no nesting of parentheses, in which case you need a language parser):
>> str = 'hello exp(2*blah) world';
>> regexprep(str,'exp\(([^\)]+)\)','e^{$1}')
ans =
hello e^{2*blah} world
Thanks again. You have peaked my curisoity now. MATLAB has inbuilt functionaly whereby I can hover over a paranthases with the cursor and it will tell me where its closed/opened. This suggests it has a way of tracking the functionality for maths purposes (as you would hope). LaTeX on the other hand wants to track the functionality for a visual for example:
exp(i*(k1-k2)*x) is the regular MATLAB expression
e^{i(\mathrm{k_1} - \mathrm{k_2})x} is the LaTeX formalism.
But even as i type this into the editor for this questions page I can see that it is tracking both the curly brackets and the parantheses and returning the coresponding closer/openeer.
Do you think it is possible to use this functionality to map the syntax.
For example:
  • I want to always take the string exp and change it to e^
  • I then want the next bracket and its corresponding closer to form a set of curly brackets ie. exp(<--.......(...[some more stuff]....)....even more stuff ...(...).....)<-- Will change to e^{...[all the middle stuff]...} where the inner bracketed expressions will be remembered but not included
  • Then input the inner expressions (or perform regular expression replacements on them) to the curly brackets in the newly set LaTeX form.
"peaked" -> piqued
"Do you think it is possible to use this functionality to map the syntax."
For that you need a language parser, which is not a trivial thing to write. Of course the best parser for the MATLAB language is MATLAB, third-party tools are unlikely to behave in the exactly the same way. AFAIK MATLAB does not offer documented access to its parser, other than calling the code.
You might be able to get one these to do most of what you want:
Thank you for a flurry of ideas and an english education. I hope that my MALAB interest (and abilitiy) is indeed piqued and not peaked.
So the latex functionality seems fantastic, I struggle with the complexity of some of the equations and including variables, but perhaps I can make a work around.
On to perhaps a more simple part of ym question:
"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."
Do you know why I can't use the variable in my code. I want to replace the string A5(1) but it may sometimes be A4(1) so programtically it is the string ['A',nstr,'(1)'] where nstr goes from 1 - 100 say. But it doesn't seem to like this and the two first lines of code do nothing for strrep or regexprep.
"But it doesn't seem to like this and the two first lines of code do nothing for strrep or regexprep."
You did not escape the parentheses in any of your regular expressions, so for example:
['A',nstr,'(1)']
e.g.: 'A5(1)'
will actually match
A51
because parentheses are grouping charcters, and are not literal characters to match. If you really want to match literal parentheses then you will need to escape them, e.g.:
['A',nstr,'\(1\)']
and repeatedly read the regular expression documentation very carefully:
Personally I would use dynamic regular expressions rather than those huge cell arrays of almost-identical match and replace character vectors, either with some simple indexing into a cell array or using an algorithm that converts from the (x) value to the \mathrm{...} content.
"or using an algorithm that converts from the (x) value to the \mathrm{...} content"
You have anticipated well my next question.
This was my initial idea essential (1) maps to \mathrm{i}, (2) maps to \mathrm{s}, (3) maps to \mathrm{p} and so on. There is no general letter pattern (they stand for idler signal and pump). So in this instance you would make a look up table, is this what you mean by:
"either with some simple indexing into a cell array" ?
Then if I do this I would really like all instance of x=1 to be a (i) and all instances of x=3 to be (p)
I will think of a way of doing this, but expect a question in the future.
Many Thanks,
Tom
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.
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개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2019년 11월 25일

댓글:

2019년 11월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by