Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Taking peices out of a sentence

1 回表示 (過去 30 日間)
Vasko Nechev
Vasko Nechev 2017 年 6 月 26 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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!

回答 (1 件)

Image Analyst
Image Analyst 2017 年 6 月 26 日
編集済み: Image Analyst 2017 年 6 月 26 日
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.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by