Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to parse a string? (Simple question)

Subject: How to parse a string? (Simple question)

From: Kian

Date: 15 Jun, 2009 02:06:01

Message: 1 of 4

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!

Subject: How to parse a string? (Simple question)

From: Sadik

Date: 15 Jun, 2009 02:39:02

Message: 2 of 4

I guess you could do this:

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

Hope this helps.


"Kian " <kian.torab@utah.edu> wrote in message <h14aa9$5r9$1@fred.mathworks.com>...
> 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!

Subject: How to parse a string? (Simple question)

From: Kian

Date: 15 Jun, 2009 02:58:03

Message: 3 of 4

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

Subject: How to parse a string? (Simple question)

From: Bruno Luong

Date: 15 Jun, 2009 09:47:01

Message: 4 of 4

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us