Indexing parts of a string that occur in every x characters

6 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2022-6-20
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 个评论
Zain
Zain 2022-6-20
I'm not sure this method will work since each data cell is separated by a different number of characters, although it doesn't look that way. Some are 8 characters long and some are 16 characters long
Walter Roberson
Walter Roberson 2022-6-21
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by