Info

This question is closed. Reopen it to edit or answer.

How to find column and line # at which a match occurs using the find function?

1 view (last 30 days)
Hi,
I have a large string and I am using the find function to find a specific word in the string. How do I know the line and the column number at which the match occurs?
Thanks
  3 Comments
Walter Roberson
Walter Roberson on 8 Nov 2013
find() returns an index. If there is only one line, then the index is the column number.
Is it possible that you are working with a cell array of strings? Or that you are working with a character array that is blank padded, such as
['Hello'; 'Edy ']
If you are then asking about lines becomes meaningful.
Asking about lines is also meaningful if your lines have a delimiter such as newline between them. For example,
S = sprintf('Hello\nEdy');
then S will a string with 9 characters in it, the 6th of which is the newline character (char(10)). You can end up with strings like that if you read in an entire text file.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!