<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253678</link>
    <title>MATLAB Central Newsreader - How to parse a string? (Simple question)</title>
    <description>Feed for thread: How to parse a string? (Simple question)</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 15 Jun 2009 02:06:01 -0400</pubDate>
      <title>How to parse a string? (Simple question)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253678#657251</link>
      <author>Kian </author>
      <description>Last question for a few days, promise.&lt;br&gt;
&lt;br&gt;
How do I pull out the numbers out of this string WITHOUT a loop?&lt;br&gt;
&lt;br&gt;
data = '*12#;*123#;*1122#;*2#;*6312#;*112#;*1251#;*1912#;*8612#;&lt;br&gt;
&lt;br&gt;
I basically want to be able to pull out the numbers, such as I'll have:&lt;br&gt;
&lt;br&gt;
markers = [12, 123, 1122, 2, 6312, 112, 1251, 1912, 8612];&lt;br&gt;
&lt;br&gt;
I can do this ONLY IF all the numbers are the same length (e.g. 3 digit numbers) using the indices of the '*'s and '#'s and bsxfun, but I don't know how to do it when the lengths are different. This is what I'd do if the lengths were the same.&lt;br&gt;
&lt;br&gt;
starIndices = find(data == '*');&lt;br&gt;
poundIndices = find(data == '#');&lt;br&gt;
&lt;br&gt;
data(bsxfun(@plus, starIndices, 1:(poundIndices-starIndices)-1);&lt;br&gt;
&lt;br&gt;
But this only will give me the first 2 digits (since the first number in the string is only 2 digits long and that bsxfun will only use that to do its thing).&lt;br&gt;
&lt;br&gt;
Any easier way to do this?&lt;br&gt;
&lt;br&gt;
Thanks a lot for your time!</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 02:39:02 -0400</pubDate>
      <title>Re: How to parse a string? (Simple question)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253678#657257</link>
      <author>Sadik </author>
      <description>I guess you could do this:&lt;br&gt;
&lt;br&gt;
markers = strread(data,'%f','delimiter','*#;');&lt;br&gt;
markers(markers==0)=[];&lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Kian &quot; &amp;lt;kian.torab@utah.edu&amp;gt; wrote in message &amp;lt;h14aa9$5r9$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Last question for a few days, promise.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; How do I pull out the numbers out of this string WITHOUT a loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; data = '*12#;*123#;*1122#;*2#;*6312#;*112#;*1251#;*1912#;*8612#;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I basically want to be able to pull out the numbers, such as I'll have:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; markers = [12, 123, 1122, 2, 6312, 112, 1251, 1912, 8612];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I can do this ONLY IF all the numbers are the same length (e.g. 3 digit numbers) using the indices of the '*'s and '#'s and bsxfun, but I don't know how to do it when the lengths are different. This is what I'd do if the lengths were the same.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; starIndices = find(data == '*');&lt;br&gt;
&amp;gt; poundIndices = find(data == '#');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; data(bsxfun(@plus, starIndices, 1:(poundIndices-starIndices)-1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But this only will give me the first 2 digits (since the first number in the string is only 2 digits long and that bsxfun will only use that to do its thing).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any easier way to do this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks a lot for your time!</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 02:58:03 -0400</pubDate>
      <title>Re: How to parse a string? (Simple question)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253678#657261</link>
      <author>Kian </author>
      <description>Thanks Sadik, BUT...&lt;br&gt;
&lt;br&gt;
I dumbed down the problem a bit to make it easier to understand. Your solution works for the problem I posted, but in reality, my data is more like this:&lt;br&gt;
&lt;br&gt;
data = '*100#*2001#*ExpParameter:T=1.000000;FSC=1.000000;O=1.000000;AR=0.000000;AFD=500.000000;HFD=1500.000000;JD=500.000000;EOX=-0.960000;EOY=-1.470000;EGX=9.000000;EGY=6.000000;FWD=1.750000;#*111#*2111#*ExpParameter:T=2.000000;FSC=2.000000;O=1.000000;AR=0.000000;AFD=500.000000;HFD=1500.000000;JD=500.000000;EOX=-0.960000;EOY=-1.470000;EGX=9.000000;EGY=6.000000;FWD=1.750000;#*122#*2221#';&lt;br&gt;
&lt;br&gt;
If you copy and paste it into MATLAB it should automatically create a variable.&lt;br&gt;
&lt;br&gt;
As you can see, I have a whole bunch of &quot;markers&quot; and &quot;experimental parameters&quot; in this one long string. Anything between a * and a # that is just a number is a &quot;marker&quot;. Anything else, is a parameter. &lt;br&gt;
&lt;br&gt;
Currently, I find the indices for all the *s. Then I check to see if the next character is a number of not. If it is, then it's a marker. Then I parse it out using the index of the next #. I do it in a loop, but I'd like to do it outside of one. Unfortunately, I'm not very familiar with some of the powerful string manipulation functions in MATLAB.&lt;br&gt;
&lt;br&gt;
Thanks for your help though. That definitely taught me a new useful function.&lt;br&gt;
&lt;br&gt;
Kian</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 09:47:01 -0400</pubDate>
      <title>Re: How to parse a string? (Simple question)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253678#657327</link>
      <author>Bruno Luong</author>
      <description>Using regular expression:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; data = '*100#*2001#*ExpParameter:T=1.000000;FSC=1.000000;O=1.000000;AR=0.000000;AFD=500.000000;HFD=1500.000000;JD=500.000000;EOX=-0.960000;EOY=-1.470000;EGX=9.000000;EGY=6.000000;FWD=1.750000;#*111#*2111#*ExpParameter:T=2.000000;FSC=2.000000;O=1.000000;AR=0.000000;AFD=500.000000;HFD=1500.000000;JD=500.000000;EOX=-0.960000;EOY=-1.470000;EGX=9.000000;EGY=6.000000;FWD=1.750000;#*122#*2221#';&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; list=str2double(regexp(data,'(?&amp;lt;=\*)((\d+)(?=\#))','match'))&lt;br&gt;
&lt;br&gt;
list =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;100        2001         111        2111         122        2221&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
  </channel>
</rss>

