Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to parse a string? (Simple question)

144 views
Skip to first unread message

Kian

unread,
Jun 14, 2009, 10:06:01 PM6/14/09
to
Last question for a few days, promise.

How do I pull out the numbers out of this string WITHOUT a loop?

data = '*12#;*123#;*1122#;*2#;*6312#;*112#;*1251#;*1912#;*8612#;

I basically want to be able to pull out the numbers, such as I'll have:

markers = [12, 123, 1122, 2, 6312, 112, 1251, 1912, 8612];

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.

starIndices = find(data == '*');
poundIndices = find(data == '#');

data(bsxfun(@plus, starIndices, 1:(poundIndices-starIndices)-1);

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).

Any easier way to do this?

Thanks a lot for your time!

Sadik

unread,
Jun 14, 2009, 10:39:02 PM6/14/09
to
I guess you could do this:

markers = strread(data,'%f','delimiter','*#;');
markers(markers==0)=[];

Hope this helps.


"Kian " <kian....@utah.edu> wrote in message <h14aa9$5r9$1...@fred.mathworks.com>...

Kian

unread,
Jun 14, 2009, 10:58:03 PM6/14/09
to
Thanks Sadik, BUT...

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:

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#';

If you copy and paste it into MATLAB it should automatically create a variable.

As you can see, I have a whole bunch of "markers" and "experimental parameters" in this one long string. Anything between a * and a # that is just a number is a "marker". Anything else, is a parameter.

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.

Thanks for your help though. That definitely taught me a new useful function.

Kian

Bruno Luong

unread,
Jun 15, 2009, 5:47:01 AM6/15/09
to
Using regular expression:

>> 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#';


>> list=str2double(regexp(data,'(?<=\*)((\d+)(?=\#))','match'))

list =

100 2001 111 2111 122 2221

% Bruno

0 new messages