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

  • Context: MATLAB 
  • Thread starter Thread starter Old Guy
  • Start date Start date
  • Tags Tags
    String Text Value
Click For Summary
SUMMARY

This discussion focuses on extracting a numeric value from a specific text string format using MATLAB. The provided example string is '10-Nov-2009_0.91304_Network_N=100_k=6_p=0.02_COMM_NETWORK', and the goal is to assign the value of N (100) to a variable. The solution involves using the findstr function to locate the position of 'N=' and the subsequent underscore, followed by the str2double function to convert the extracted substring into a numeric value. This method is effective as long as the input string consistently follows the specified format.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of string manipulation functions in MATLAB
  • Knowledge of the findstr function for locating substrings
  • Experience with data type conversion using str2double
NEXT STEPS
  • Explore advanced string manipulation techniques in MATLAB
  • Learn about regular expressions in MATLAB for more complex string patterns
  • Investigate error handling in MATLAB when dealing with string extraction
  • Study the use of regexp for extracting substrings based on patterns
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and anyone involved in text processing and extraction tasks within MATLAB environments.

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!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K