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

In summary, the conversation involves a request for program code that can extract a specific part of a text string. The code provided utilizes the findstr function to determine the position of the desired string and then uses the str2double function to convert it to a numerical value. The person requesting the code expresses their gratitude for the solution and mentions a new understanding they have gained about string manipulation in Matlab.
  • #1
Old Guy
103
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
  • #2
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...
 
  • #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!
 

1. What is meant by "extracting part of a text string"?

"Extracting part of a text string" refers to selecting a specific portion of a larger text or string of characters. This could be a word, phrase, or even a single character within the text.

2. Why would one need to extract a part of a text string?

There are many reasons why someone may need to extract part of a text string. Some common use cases include manipulating data in a spreadsheet, searching for specific information within a document, or formatting text in a more organized manner.

3. What does "find the value of N" mean in this context?

In this context, "N" represents a variable that stands for the specific part of the text string that you want to extract. It could be a number, a word, or any other character or combination of characters.

4. How can one extract a part of a text string?

There are different methods for extracting part of a text string depending on the programming language or software being used. Generally, it involves using built-in functions or methods that allow you to specify the desired part of the string and extract it.

5. Is it possible to extract part of a text string without coding or using specialized software?

Yes, it is possible to extract part of a text string without coding or using specialized software. For example, in a spreadsheet program like Microsoft Excel, you can use the "Text to Columns" feature to extract specific parts of a text string. However, using coding or specialized software may provide more flexibility and options for extracting desired portions of text.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Calculus and Beyond Homework Help
Replies
14
Views
1K
  • Introductory Physics Homework Help
Replies
16
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top