How can I parse a character matrix for individual elements without using TEXTSCAN iteratively?
9 次查看(过去 30 天)
显示 更早的评论
TEXTSCAN can only read one line at a time if the input is a string or a character matrix. If I have an array, with several thousand lines of strings, I do not want to run it iteratively through TEXTSCAN and I do not want to have to write it out to a file before I can read it in again.
I would like to know if there is a way to parse all the rows without using a FOR loop.
采纳的回答
MathWorks Support Team
2009-6-27
One solution to this issue is to use a regular expression to parse the character matrix.
Suppose we start with the following 2xN character array, where N is the number of characters in each row.
A = ['abc 46 6 ghi'; 'def 7 89 jkl'];
As a first step, we use the REGEXP command to parse array 'A' for numeric-valued strings, including floating point values with the following regular expression:
y = regexp(cellstr(A),'\d+','match')
The output 'y' contains the numeric data as a cell array:
y =
{1x2 cell}
{1x2 cell}
Next, you can convert this cell array to a numeric one using the STR2NUM command and then work on your application without having to write out to a file or iterate through rows to use TEXTREAD or SSCANF.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!