bit-question
bit-question

Reputation: 3843

strfind for string array in MATLAB

I have a cell array, x, and each row of which is a string array. For instance,

>>ab = x{2};
>>ab
ans =
  910        Flow’S Equation

Clearly, this string has a pattern: a numerical value and a text string are separated by some empty spaces. With respect to this example, when I type

>> ab(2)
  ans =
    1
>> ab(3)
  ans =
    0
>> ab(6)
  ans =
    F

In my code, I need automatically check the starting position of text string, which is the one for "F" and the end position of numerical value, which is the one for "0". I use

>> x = strfind(ab, ' ');

For this example, I expect it give me the positions of "space" as

4 5 12

In stead, it just gives me 12 without outputing 4 and 5

I think the problem is that the fourth and fifth entries of ab are not "space", How can I know what are they? When I type

>> ab(4)
 ans =

The output is nothing, just like what "space" is?

Upvotes: 0

Views: 4968

Answers (2)

grantnz
grantnz

Reputation: 7423

Try :

double(ab(4))

It's probably a non-printable ascii character

Another way you can see what is in the variable is

sprintf('[%d]',sprintf('Some\n\r\ttext'))
ans =

[83][111][109][101][10][13][9][116][101][120][116]

or in your case

ans =

sprintf('[%d]', ab );

If you're trying to find words in the string that are separated by a space or tab character you can use:

regexp(x{2},'[\t ]','split') 

Upvotes: 0

YYC
YYC

Reputation: 1802

To find out what char ab(4) is, convert the char to numerical form by

double( ab(4) )

For double(ab(4))=9, it's a TAB. If your string is ASCII, you may want to check ASCII control characters and ASCII printable characters for the mapping.

However, to find the beginning of the text string, using regexp may be a better idea:

string_begin = min( regexp(ab, '[^\d\s]') )

regexp(ab, '[^\d\s]') returns the locations of all characters that are neither numbers nor white spaces, and the minimum of the locations should be where the text begins.

Upvotes: 2

Related Questions