How to split the string with Mathematical operators?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Harsha M V
il 14 Apr 2019
Commentato: Harsha M V
il 20 Apr 2019
I have equations:
a = '(Id_M5)/Cc'
b =' (VDD-VSS)*(Id_M5+(2*(Id_M7)))'
How to split these?
I want answers like:
a = [Id_M5 Cc]
b = [VDD VSS Id_M5 2 Id_M7]
0 Commenti
Risposta accettata
Walter Roberson
il 14 Apr 2019
Where S is the string to be processed, then
output = regexp(S, '[ ()^*+/-]+', 'split');
output(cellfun(@isempty, output)) = []; %for technical reasons, there can be empty elments in the regexp output
Inside the [] you would list all of the operations that you want to have cause a split. The order matters a bit: do not put the ^ first, and the - needs to be either first or last. Only the operations you put in the [] will trigger splitting -- so for example if the user had \ then it would not split there because \ is not in the list (use \\ to represent \) and if the user had .* for multiplication then the . would not trigger splitting but the * would.
If what you really want is to extract all of the constants and identifiers, then consider instead using
regexp(S, '\W+', 'split')
That will trigger splitting on any "non-word-forming" character. The "word-forming characters" are the digits, upper and lower case letters, and the underscore. Note that period is not a "word-forming character".
If you want the MATLAB .* and .^ and ./ operators to also cause splitting, then you could consider adding period to the list inside the [], but you need to think about whether the user is intended to be able to enter floating point constants like 2.5 . Handling floating point numbers correctly is a bit tricky: is 2.^3 intended to convey floating 2.0 raised to the power 3, or is it intended to convey integer 2 raised element-wise to the power of 3? In MATLAB, 2..^.3 is a valid expression conveying floating point 2.0 raised element-wise to the power floating point 0.3 .
If you need to handle floating point, then it is best to specify exactly which floating point forms are to be permitted. Do you permit 'd' floating point as well as 'e' floating point? 3.5e2 -> 350, 3.5d2 -> 350 ? MATLAB does. How about complex numbers? When is 1+2i to be split as-if the + is addition, versus when it is recognized as number-forming. Does 1+2i^2 indicate complex(1,2)^2, (1+2i)^2, or does it represent 1+(2i)^2 ?
4 Commenti
Walter Roberson
il 19 Apr 2019
If you mean that you would want a list of the values associated with the variables named in the string then there are multiple options.
One way is to put all of permitted variables as named fields in a structure, and then use either getfield() or dynamic structure field names.
Another approach is put all of the permitted variables as key names and associated values in a container.Map
If you want to be able to get the current value of all variables that exist in your program then chances are high that you would be making a mistake. There is no good reason for a user to probe your loop control variables.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Whos 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!