Picking out a specific string within a string variable.
1 次查看(过去 30 天)
显示 更早的评论
I have string variable of decimal numbers with spaces in between each...
HoldingLine = 10 78 984182400 2750 0.1224 0.1873 0.2511 0.2340 0.1406 0.0646 380 0.1341 0.1877 0.2494 0.2236 0.1251 0.0544 40 40 40 40 5.1 5.1 5.1 5.1 9.4 9.4 9.4 9.4 9.6 9.6 9.6 9.6 2.7 2.7 2.7 2.7 40 40 40 40 63 50 63 62 3.3 3.3 3.3 3.3 37 37 37 37 5.0 5.0 5.0 5.0 71 71 72 72 3.3 3.3 3.3 3.3
I used the following to get an array of all the 'space' locations...
Position=find(isspace(HoldingLine));
This outputs the following (which gives me the location of my zeros)...
Position =
Columns 1 through 15
3 6 16 21 28 35 42 49 56 63 67 74 81 88 95
Columns 16 through 30
102 109 112 115 118 121 125 129 133 137 141 145 149 153 157 ......
Now I want to print a specifc number in HoldingLine, such as the third number phrase (984182400). Any ideas on how to print just that number (and other specific numbers) from the variable HoldingLine. I see that you can use fseek and fread to do this from a text file, but this is a variable.
0 个评论
采纳的回答
Walter Roberson
2011-3-30
Probably the easiest thing would be to use
T = regexp(HoldingLine,' ','split');
T{3}
0 个评论
更多回答(1 个)
Ryan
2011-3-30
2 个评论
Walter Roberson
2011-3-30
Or to be more specific, to extract the K'th word,
tP = [0, Position, length(HoldingLine)+1];
T = HoldingLine(tP(K+1)+1:tP(K+2)-1);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!