How do I replace/manipulate characters in a string? Specifically integers in a string...

If I have a string like axiom = 'F18-F4+F'; I need to manipulate the string so that it reads as follows: F------------------F++++F
Basically I need for the numbers in the string to behave as integer multipliers on the + and - sign.
I have tried many combinations and approaches using regex, str2Double, isstrprop(axiom, 'alpha'); Anyone have any advice on this kind of string manipulation? Thanks in advance!

2 Comments

I've no klew what the "I need" expression is intended to mean?
Give us the rule for how to generate the output from the input.
In the expression axiom = 'F18-F4+F'; 18 and 4 are multipliers on the - and + signs. I need to manipulate the expression so that it reads F------------------F++++F. The rules are: 1) Loop over the string 2) if a position in the string is an integer it should act as a multiplier on the following plus or minus sign. Does that help? I apologize for the lack of clarity. I made edits to my post above as well. Thanks @dpb!

Sign in to comment.

 Accepted Answer

One easy way with regexprep:
>> str = 'F18-F4+F';
>> regexprep(str,'(\d+)([+-])','${repmat($2,1,str2double($1))}')
ans =
F------------------F++++F

1 Comment

You are a boss! Thanks so much! I am new to matlab and taking a graduate course and still trying to figure out the easiest things! Thanks again!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 29 Jan 2017

Commented:

on 29 Jan 2017

Community Treasure Hunt

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

Start Hunting!