Indexing parts of a string that occur in every x characters
6 views (last 30 days)
Show older comments
I have a string of data that goes on for a while. I want to index a certain value that occurs after the same number of characters because of the way the data is set up. Here's the string:
'GRID* 5250001 5200000 0.730198500E+02-0.177000000E+03* -0.512716000E+02 5200000 0 0GRID* 5250002 5200000 0.730198500E+02-0.176250000E+03* -0.512716000E+02 5200000 0 0...'
Say I want to extract the value after it says 'GRID*' every time. I want to write something to start indexing at the 17th character, extract the following 8 characters, then repeat the same process again 144 characters later to get the value next to where it says 'GRID*' for the second time, all the way until the end of the string. I thought it would be simple indexing, but I can't figure it out. Thanks in advance!
0 Comments
Accepted Answer
Walter Roberson
on 20 Jun 2022
convert the string to char, reshape to 144 rows, extract row 17:17+8-1, all columns. You might want to transpose the result from columns to row.
3 Comments
Walter Roberson
on 21 Jun 2022
If there is a fixed separation between GRID* then reshaping should work. Extract the maximum number of characters per entry, as columns, such as 17:17+16-1. Transpose, string() or cellstr() and post process to decide the proper size for each entry.
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!