Extracting certain elements of a string (Matlab)

In summary, the conversation discusses using the "findstr" or "strfind" functions in Matlab to identify the start and end points of a string within a cell and then extracting specific characters from that string. The use of the "extractBefore", "extractBetween", or "extractAfter" functions is also mentioned.
  • #1
big man
254
1
Hi

I am writing a program in Matlab and I want to search a string within a cell to identify where certain characters start and end. However, once I identify the start and end points I want to extract all the characters within that range.

So for example let's say that I have a 25 row by 1 column cell array called Carray. Let's also say that I'm only interested in the string that is within the 25th row cell. What I'm wanting to do is use something like the "findstr" or "strfind" functions in Matlab to give the the start and finish indices for a string like "bleh". Then I want to be able to take those four characters only from the entire string that is contained in the 25th row.

if i==25
bleh=strfind(able, 'CTDI')
bleh1=strfind(able, 'DLP')
% new able = extract the characters between the subindices bleh and bleh1 from cell able(25)
end

Really all I need to know is if there is a way to specify subindices or something within the able array so that only certain characters in a particular cell are extracted?

I'd really appreciate any advice in this matter and I hope that I have managed to explain it OK!

Thank you
 
Physics news on Phys.org
  • #2
First convert the element in the cell array to string: str = convertCharsToStrings(able(i)).

Then use strfind as you've used.

Then use extractBefore, extractBetween or extractAfter functions as required, on str.
 

FAQ: Extracting certain elements of a string (Matlab)

1. How can I extract specific characters from a string in Matlab?

To extract specific characters from a string in Matlab, you can use the strfind or regexp functions. These functions allow you to search for a specific pattern or sequence of characters within a string and return the position of the match.

2. Can I extract a substring from the middle of a string in Matlab?

Yes, you can extract a substring from the middle of a string in Matlab using the substr function. This function allows you to specify the starting index and the length of the substring you want to extract.

3. How do I extract numbers from a string in Matlab?

To extract numbers from a string in Matlab, you can use the str2num function. This function converts a string into a numeric array by removing any non-numeric characters. You can then use indexing to extract specific numbers from the resulting array.

4. Is it possible to extract multiple elements from a string in Matlab?

Yes, it is possible to extract multiple elements from a string in Matlab. You can use the strsplit function to split a string into multiple substrings based on a delimiter. You can also use the regexp function with a regular expression to extract multiple patterns from a string.

5. How can I extract characters from a string based on a condition in Matlab?

You can extract characters from a string based on a condition in Matlab by using the regexp function with a regular expression. Regular expressions allow you to define a pattern that the characters must match in order to be extracted. You can also use logical indexing to extract characters based on a condition.

Similar threads

Replies
3
Views
2K
Replies
1
Views
2K
Replies
1
Views
13K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
3
Views
1K
Replies
1
Views
3K
Replies
3
Views
2K
Replies
1
Views
3K
Back
Top