Picking out a specific string within a string variable.

1 view (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Mar 2011
Probably the easiest thing would be to use
T = regexp(HoldingLine,' ','split');
T{3}

More Answers (1)

Ryan
Ryan on 30 Mar 2011
Ahhh, I just figured it out, haha.
HoldingLine(6:16)
ans =
984182400
  2 Comments
Walter Roberson
Walter Roberson on 30 Mar 2011
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);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!