Extracting certain elements of a string (Matlab)

Click For Summary
SUMMARY

The discussion focuses on extracting specific characters from a string within a cell array in Matlab. The user aims to identify start and end indices using the "strfind" function and then extract the substring between these indices from the 25th row of a cell array called Carray. The solution involves converting the cell element to a string using "convertCharsToStrings" and then utilizing "extractBefore," "extractBetween," or "extractAfter" functions for the extraction process.

PREREQUISITES
  • Familiarity with Matlab programming language
  • Understanding of cell arrays in Matlab
  • Knowledge of string manipulation functions in Matlab
  • Experience with indexing in Matlab
NEXT STEPS
  • Learn how to use "convertCharsToStrings" in Matlab
  • Explore the "extractBefore" function for string manipulation
  • Research the "extractBetween" function for substring extraction
  • Investigate the "extractAfter" function for advanced string operations
USEFUL FOR

Matlab programmers, data analysts, and anyone working with string manipulation in cell arrays will benefit from this discussion.

big man
Messages
242
Reaction score
0
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
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
13K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K