MATLAB Answers


How to extract data from a srting?

Asked by Vinay kumar Reddy on 31 Jan 2013


*I have this data as a single string *

Physical Location (City):Virginia

Manager of this associate:Donna Harding

Start Date:1-1-99

ID Type:Logon for SUPERVALU associate

For Transfer Requests From Dept:

Transfer Date:

Previous UserID:

Departure Date:

Middle Name:

I want to extract the name of the manager from the

Manager of this associate:Donna Harding

I mean I want data in the above string after : to be stored in other variable.

Could anyone help me in doing this?

Thank You


Jan Simon
on 31 Jan 2013

Please post the input in valid Matlab syntax. If matters if this is a cell string, or if the lines are separated by CHAR(10) linebreaks, or if they are not separated, or if this "string" is written to a file. Does the string contain a blank line after each line of text or does this occur in the forum only? As soon as the data are defined by valid Matlab syntax, all these questions implicitly vanish.

Hi The posted one is the matlab input. I got a cell array,in which each cell has entire above mentioned data. each cell has different attribute for manager of the associate field.

and it has a empty line between every two lines. Thank You

Jan Simon
on 31 Jan 2013

This description as a text does not enlighten me. What is "the Matlab input"? Each cell has the data? Do you mean a cell string? With one line per cell element, or with one string and line breaks?

Again I suggest to post Matlab code due to the unbeatable clarity.


No products are associated with this question.

2 Answers

Answer by Cedric Wannaz
on 31 Jan 2013
Edited by Cedric Wannaz
on 1 Feb 2013
 Accepted answer

Solution with a regexp:

 regexp(s, '(?<=associate:).*(?=Start)', 'match')

where s contains your string.

EDIT: the following would work as well..

 regexp(s, '(?<=[^)]:).*(?=Start Date)', 'match')

..does anybody have a simpler pattern proposal?

  1 Comment

Thanks!!! Cedric

That works great!!!

Answer by Matt J
on 31 Jan 2013
Edited by Matt J
on 31 Jan 2013

   b='Manager of this associate';
   e='Start Date';


Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!