Info

This question is closed. Reopen it to edit or answer.

Taking peices out of a sentence

1 view (last 30 days)
Vasko Nechev
Vasko Nechev on 26 Jun 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
Could anyone help me figure out how to do this?
I have a large doc full of sentences in the exact format of (1). I need to create a loop that will take (1) and turn it into (2) over and over.
I have no problem creating the loop I just don't know how to select the number from the first part out of the sentence (number always has same length and position).
(1) DD 11011 WELD TO BIKE FRAME
(2) blahblah = *"DD 11011 WELD TO BIKE FRAME"* new line then tab once *"DD11011"* blahblahblah
And finally I'm am not totally sure if matlab has a tab option with fprintf?
Thanks so much for any help!

Answers (1)

Image Analyst
Image Analyst on 26 Jun 2017
Edited: Image Analyst on 26 Jun 2017
If thisLine is your line of text and there are spaces around the number, try this inside your loop:
thisLine = 'DD 11011 WELD TO BIKE FRAME' % Changes on each loop iteration.
spaceLocations = find(thisLine == ' ')
letterCode = thisLine(1:spaceLocations(1)-1)
numberCode = thisLine(spaceLocations(1)+1:spaceLocations(2)-1)
fprintf('blahblah = "%s"\n\t"%s%s" blahblahblah\n', thisLine, letterCode, numberCode);
Or if the letters always go from index 1 to 2, and the numbers from index 4 to 8, you could simplify it to
thisLine = 'DD 11011 WELD TO BIKE FRAME'
fprintf('blahblah = "%s"\n\t"%s%s" blahblahblah\n', thisLine, thisLine(1:2), thisLine(4:8));
but the first code is probably more robust.

This question is closed.

Community Treasure Hunt

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

Start Hunting!