<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: MATLAB, Strings, and Regular Expressions</title>
	<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/</link>
	<description>Loren Shure  works on design of the MATLAB language at &#60;a href="http://www.mathworks.com/"&#62;The MathWorks&#60;/a&#62;. She writes here about once a week on MATLAB programming and related topics. &#60;br&#62;&#60;br&#62;&#60;a href="/images/loren-full.jpg"&#62;&#60;img src="/images/loren.jpg"&#62;&#60;/a&#62;</description>
	<pubDate>Sun, 08 Nov 2009 03:55:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30707</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 27 Oct 2009 17:06:17 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30707</guid>
		<description>Alexander-

&lt;a href="http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html" rel="nofollow"&gt;The documentation&lt;/a&gt; covers what MATLAB supports.

--Loren</description>
		<content:encoded><![CDATA[<p>Alexander-</p>
<p><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html" rel="nofollow">The documentation</a> covers what MATLAB supports.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alexander Panchenko</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30706</link>
		<dc:creator>Alexander Panchenko</dc:creator>
		<pubDate>Tue, 27 Oct 2009 16:22:07 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30706</guid>
		<description>Could you please give a pointer so the documentation of the language of the regular expresions supported in Matlab where one can see to what extend it supports the Perl dialect?</description>
		<content:encoded><![CDATA[<p>Could you please give a pointer so the documentation of the language of the regular expresions supported in Matlab where one can see to what extend it supports the Perl dialect?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Breslau</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30250</link>
		<dc:creator>Jason Breslau</dc:creator>
		<pubDate>Wed, 29 Apr 2009 13:40:50 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30250</guid>
		<description>Hi JB,

You need to modify your end-of-file check to be at the beginning of your while loop, not at the end.

The error you are getting is because you are passing -1 (end-of-file) to regexp.

-=&#62;J</description>
		<content:encoded><![CDATA[<p>Hi JB,</p>
<p>You need to modify your end-of-file check to be at the beginning of your while loop, not at the end.</p>
<p>The error you are getting is because you are passing -1 (end-of-file) to regexp.</p>
<p>-=&gt;J</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30248</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 29 Apr 2009 11:05:12 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30248</guid>
		<description>Here's the corrected link for &lt;a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#f0-56360" rel="nofollow"&gt;tokens&lt;/a&gt;.

--Loren</description>
		<content:encoded><![CDATA[<p>Here&#8217;s the corrected link for <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#f0-56360" rel="nofollow">tokens</a>.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JB</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30246</link>
		<dc:creator>JB</dc:creator>
		<pubDate>Wed, 29 Apr 2009 11:00:54 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30246</guid>
		<description>simplifying:
fid = fopen('ascii.txt');
while 1
    tline = fgetl(fid);
    pat = ' first=\s+(\d+)';
    a = regexp(tline,pat,'match');
    a(:)
    pat = '\d+';
    c = regexp(a,pat,'match');
    c(:)
    if ~ischar(tline),   break,   end
end
fclose(fid);
~~~~~~~~~~~~~~
produces
ans = 
    ' first=  325'
ans = 
    {1x1 cell}
ans = 
    ' first=  325'
ans = 
    {1x1 cell}
??? Undefined function or method 'regexp' for input arguments of type 'double'.
Error in ==&#62; reg at 7
    a = regexp(tline,pat,'match');

What's the appropriate, elegant way to get at the numeric value?</description>
		<content:encoded><![CDATA[<p>simplifying:<br />
fid = fopen(&#8217;ascii.txt&#8217;);<br />
while 1<br />
    tline = fgetl(fid);<br />
    pat = &#8216; first=\s+(\d+)&#8217;;<br />
    a = regexp(tline,pat,&#8217;match&#8217;);<br />
    a(:)<br />
    pat = &#8216;\d+&#8217;;<br />
    c = regexp(a,pat,&#8217;match&#8217;);<br />
    c(:)<br />
    if ~ischar(tline),   break,   end<br />
end<br />
fclose(fid);<br />
~~~~~~~~~~~~~~<br />
produces<br />
ans =<br />
    &#8216; first=  325&#8242;<br />
ans =<br />
    {1&#215;1 cell}<br />
ans =<br />
    &#8216; first=  325&#8242;<br />
ans =<br />
    {1&#215;1 cell}<br />
??? Undefined function or method &#8216;regexp&#8217; for input arguments of type &#8216;double&#8217;.<br />
Error in ==&gt; reg at 7<br />
    a = regexp(tline,pat,&#8217;match&#8217;);</p>
<p>What&#8217;s the appropriate, elegant way to get at the numeric value?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JB</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30245</link>
		<dc:creator>JB</dc:creator>
		<pubDate>Wed, 29 Apr 2009 10:51:19 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30245</guid>
		<description>Please, enlighten me as to eleganlty peform the following:

I have an ascii text file (notepad) with lines of values with the following format. 

first= 320 second= 234566 third= -0.24  ... 
first= 788 second= 256667 third= -0.24  ... 
and so on.

I anticipated using code like the following to create a matrix of the numeric values in my text file that match my patterns.

fid = fopen('accuSUMok.txt');
i = 1;
while 1
    tline = fgetl(fid);
    pat = ' first=\s+\d+';
    a = regexp(tline,pat,'match');
    disp(a);
    pat = '\d+';
    c = regexp(a,pat,'match');
    disp(c);
    pat = ' third=\s+\-0\.\d+';
    b = regexp(tline,pat,'match');
    disp(b);
    pat ='\-0\.\d+';
    d = regexp(b,pat,'match');
    disp(d);
    j = [ a c b d]
    %printf('%s %s', a, b, c, d);
    if ~ischar(tline),   break,   end
end
fclose(fid);

But what I see is, many lines of, with the error at end of file.:

    ' first=  325'

    {1x1 cell}

    ' third=  -0.36'

    {1x1 cell}

??? Undefined function or method 'regexp' for input arguments of type 'double'.

Error in ==&#62; reg at 7
    a = regexp(tline,pat,'match');

What can I do to elegantly put the numeric values into an array?
tia</description>
		<content:encoded><![CDATA[<p>Please, enlighten me as to eleganlty peform the following:</p>
<p>I have an ascii text file (notepad) with lines of values with the following format. </p>
<p>first= 320 second= 234566 third= -0.24  &#8230;<br />
first= 788 second= 256667 third= -0.24  &#8230;<br />
and so on.</p>
<p>I anticipated using code like the following to create a matrix of the numeric values in my text file that match my patterns.</p>
<p>fid = fopen(&#8217;accuSUMok.txt&#8217;);<br />
i = 1;<br />
while 1<br />
    tline = fgetl(fid);<br />
    pat = &#8216; first=\s+\d+&#8217;;<br />
    a = regexp(tline,pat,&#8217;match&#8217;);<br />
    disp(a);<br />
    pat = &#8216;\d+&#8217;;<br />
    c = regexp(a,pat,&#8217;match&#8217;);<br />
    disp(c);<br />
    pat = &#8216; third=\s+\-0\.\d+&#8217;;<br />
    b = regexp(tline,pat,&#8217;match&#8217;);<br />
    disp(b);<br />
    pat =&#8217;\-0\.\d+&#8217;;<br />
    d = regexp(b,pat,&#8217;match&#8217;);<br />
    disp(d);<br />
    j = [ a c b d]<br />
    %printf(&#8217;%s %s&#8217;, a, b, c, d);<br />
    if ~ischar(tline),   break,   end<br />
end<br />
fclose(fid);</p>
<p>But what I see is, many lines of, with the error at end of file.:</p>
<p>    &#8216; first=  325&#8242;</p>
<p>    {1&#215;1 cell}</p>
<p>    &#8216; third=  -0.36&#8242;</p>
<p>    {1&#215;1 cell}</p>
<p>??? Undefined function or method &#8216;regexp&#8217; for input arguments of type &#8216;double&#8217;.</p>
<p>Error in ==&gt; reg at 7<br />
    a = regexp(tline,pat,&#8217;match&#8217;);</p>
<p>What can I do to elegantly put the numeric values into an array?<br />
tia</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30151</link>
		<dc:creator>David</dc:creator>
		<pubDate>Thu, 26 Mar 2009 15:28:00 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-30151</guid>
		<description>strread</description>
		<content:encoded><![CDATA[<p>strread</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Breslau</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-29688</link>
		<dc:creator>Jason Breslau</dc:creator>
		<pubDate>Tue, 12 Aug 2008 13:46:50 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-29688</guid>
		<description>MATLAB's regular expressions are very similar to what you will find described in "Mastering Regular Expressions".  RegexBuddy supports many flavors, the most similar to MATLAB being Perl and Java.  I highly recommend the "Mastering Regular Expressions" book.  The book mentions what features and syntaxes are flavor specific, and often presents tables of the various languages and their support of a specific feature.  MATLAB's syntax will generally be one of those.  The details of MATLAB's regular expressions are found here: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html

-=&#62;J</description>
		<content:encoded><![CDATA[<p>MATLAB&#8217;s regular expressions are very similar to what you will find described in &#8220;Mastering Regular Expressions&#8221;.  RegexBuddy supports many flavors, the most similar to MATLAB being Perl and Java.  I highly recommend the &#8220;Mastering Regular Expressions&#8221; book.  The book mentions what features and syntaxes are flavor specific, and often presents tables of the various languages and their support of a specific feature.  MATLAB&#8217;s syntax will generally be one of those.  The details of MATLAB&#8217;s regular expressions are found here: <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html" rel="nofollow">http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html</a></p>
<p>-=&gt;J</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: per isakson</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-29684</link>
		<dc:creator>per isakson</dc:creator>
		<pubDate>Tue, 12 Aug 2008 11:15:25 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-29684</guid>
		<description>The flavour of regular expressions, which is implemented in Matlab, is that identical (or close) to a flavour that one can find i a book like "Mastering Regular Expressions" or supported by a tool like RegexBuddy, http://www.regexbuddy.com/?</description>
		<content:encoded><![CDATA[<p>The flavour of regular expressions, which is implemented in Matlab, is that identical (or close) to a flavour that one can find i a book like &#8220;Mastering Regular Expressions&#8221; or supported by a tool like RegexBuddy, <a href="http://www.regexbuddy.com/?" rel="nofollow">http://www.regexbuddy.com/?</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Breslau</title>
		<link>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-28236</link>
		<dc:creator>Jason Breslau</dc:creator>
		<pubDate>Thu, 10 Apr 2008 14:06:14 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/04/05/regexp-how-tos/#comment-28236</guid>
		<description>Hi Rob,

My initial thought was that you should use textscan for your problem.  Unfortunately, textscan will stop processing if it encounters data that it can not convert, as opposed to the NaNs that you want.

You can get your result using a couple of splits:

Create some sample data:

&#62;&#62; str = sprintf('123 &#124; 456 &#124; 6.4&#124;asdd\n124 &#124; 457 &#124; 6.8&#124;asdd\n125 &#124; 458 &#124; 7.0&#124;qwerty')
str =
123 &#124; 456 &#124; 6.4&#124;asdd
124 &#124; 457 &#124; 6.8&#124;asdd
125 &#124; 458 &#124; 7.0&#124;qwerty

Split the data on newlines to find the rows:

&#62;&#62; rows = regexp(str, '\n', 'split')
rows = 
    [1x20 char]    [1x20 char]    [1x22 char]

Now split the rows into individual cells:

&#62;&#62; cells = regexp(rows, '\s*\&#124;\s*', 'split')
cells = 
    {1x4 cell}    {1x4 cell}    {1x4 cell}

Since cells is now a 1x3 cell of 1x4 cells, vertically concatenate them to recreate the original shape of the data:

&#62;&#62; cells = vertcat(cells{:})
cells = 
    '123'    '456'    '6.4'    'asdd'  
    '124'    '457'    '6.8'    'asdd'  
    '125'    '458'    '7.0'    'qwerty'

Now str2double will do the rest of the work:

&#62;&#62; str2double(cells)
ans =
  123.0000  456.0000    6.4000       NaN
  124.0000  457.0000    6.8000       NaN
  125.0000  458.0000    7.0000       NaN

-=&#62;J</description>
		<content:encoded><![CDATA[<p>Hi Rob,</p>
<p>My initial thought was that you should use textscan for your problem.  Unfortunately, textscan will stop processing if it encounters data that it can not convert, as opposed to the NaNs that you want.</p>
<p>You can get your result using a couple of splits:</p>
<p>Create some sample data:</p>
<p>&gt;&gt; str = sprintf(&#8217;123 | 456 | 6.4|asdd\n124 | 457 | 6.8|asdd\n125 | 458 | 7.0|qwerty&#8217;)<br />
str =<br />
123 | 456 | 6.4|asdd<br />
124 | 457 | 6.8|asdd<br />
125 | 458 | 7.0|qwerty</p>
<p>Split the data on newlines to find the rows:</p>
<p>&gt;&gt; rows = regexp(str, &#8216;\n&#8217;, &#8217;split&#8217;)<br />
rows =<br />
    [1&#215;20 char]    [1&#215;20 char]    [1&#215;22 char]</p>
<p>Now split the rows into individual cells:</p>
<p>&gt;&gt; cells = regexp(rows, &#8216;\s*\|\s*&#8217;, &#8217;split&#8217;)<br />
cells =<br />
    {1&#215;4 cell}    {1&#215;4 cell}    {1&#215;4 cell}</p>
<p>Since cells is now a 1&#215;3 cell of 1&#215;4 cells, vertically concatenate them to recreate the original shape of the data:</p>
<p>&gt;&gt; cells = vertcat(cells{:})<br />
cells =<br />
    &#8216;123&#8242;    &#8216;456&#8242;    &#8216;6.4&#8242;    &#8216;asdd&#8217;<br />
    &#8216;124&#8242;    &#8216;457&#8242;    &#8216;6.8&#8242;    &#8216;asdd&#8217;<br />
    &#8216;125&#8242;    &#8216;458&#8242;    &#8216;7.0&#8242;    &#8216;qwerty&#8217;</p>
<p>Now str2double will do the rest of the work:</p>
<p>&gt;&gt; str2double(cells)<br />
ans =<br />
  123.0000  456.0000    6.4000       NaN<br />
  124.0000  457.0000    6.8000       NaN<br />
  125.0000  458.0000    7.0000       NaN</p>
<p>-=&gt;J</p>
]]></content:encoded>
	</item>
</channel>
</rss>
