Extracting Part of a Text String - Find the Value of N

  • Topic: MATLAB 
  • Thread starter Thread starter Old Guy
  • Start date Start date
  • Tags Tags
    String Text Value
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
2 replies · 2K views
Old Guy
Messages
101
Reaction score
1
I need program code that will extract part of a text string. For example, I have the following text string:
'10-Nov-2009_0.91304_Network_N=100_k=6_p=0.02_COMM_NETWORK' and I want to wirte code that will result in a command N=100 (ie, assigning a variable N the value of 100).

I have figured out how I can determine the numeric position of the string using findstr, but how can I get the 100 out? Thanks.
 
Physics news on Phys.org
How consistent is your input string? I mean, will it always be N=XX_ so that the number your interested in is always between N= and an underscore?

If so, this should work:

%Find the position of 'N='...
NEIndex = findstr('N=',textString);
%Find all of the underscores...
uscoreIndex = findstr('_',textString);
%Find the first underscore after 'N='...
uscoreIndex = uscoreIndex(uscoreIndex>NEIndex);
%Set N...
N = str2double(textString(NEIndex+2:uscoreIndex(1)-1));

Let me know if that makes sense - hope that helps...
 
Thank you; this is perfect. The bit of knowledge that I didn't have, and couldn't find in Matlab Help, was that for a string A, the code A(n:m) picks out the characters in the string from position n to m. Thanks again!