Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab question

  1. Nov 12, 2009 #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.
  2. jcsd
  3. Nov 12, 2009 #2


    User Avatar
    Science Advisor
    Gold Member

    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...
  4. Nov 13, 2009 #3
    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!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook