how to use regexp in a more complex example

조회 수: 2 (최근 30일)
Leonardo Wayne
Leonardo Wayne 2016년 4월 19일
편집: Stephen23 2016년 4월 19일
This MATLAB code returns an array called "selected motors" at line 23 via user entry.
I would like to get all the indices(row number) of cells in raw2 1st column that have the string 1-2-3 and the selected motors values in them.

채택된 답변

Stephen23
Stephen23 2016년 4월 19일
편집: Stephen23 2016년 4월 19일
In two regexp calls:
>> selected_motor = [1111,4444];
>> C = {'1111 1-2-3';'2222 1-2-3';'3333 4-5-6';'4444 7-8-9';'4444 1-2-3'}
C =
'1111 1-2-3'
'2222 1-2-3'
'3333 4-5-6'
'4444 7-8-9'
'4444 1-2-3'
>> tmp = sprintf('|%d',selected_motor);
>> idx = ~cellfun('isempty',regexp(C,'1-2-3$','once'));
>> idy = ~cellfun('isempty',regexp(C,sprintf('^(%s)',tmp(2:end)),'once'));
>> C(idx & idy)
ans =
'1111 1-2-3'
'4444 1-2-3'
or one regexp call:
>> idz = ~cellfun('isempty',regexp(C,sprintf('^(%s) 1-2-3$',tmp(2:end)),'once'));
>> C(idz)
ans =
'1111 1-2-3'
'4444 1-2-3'
If you wish to experiment with regular expressions, then you can use my FEX submission Regular Expression Helper to create and change regular expressions in real time, and see the outputs in real time:
  댓글 수: 2
Leonardo Wayne
Leonardo Wayne 2016년 4월 19일
Thanks for your help. But first a few questions if you could answer:
1- what do the following lines do?
tmp = sprintf('|%d',selected_motor);
idy = ~cellfun('isempty',regexp(C,sprintf('^(%s)',tmp(2:end)),'once'));
I did understand that you have brilliantly combined idx and idy together to find the cells. But what I really want is to find the indices(rows) of these cells. In other words index(row numbers) of '1111 1-2-3' and '4444 1-2-3' in the original cell "C" as in the example above.
Stephen23
Stephen23 2016년 4월 19일
편집: Stephen23 2016년 4월 19일
If you want the subscript indices (i.e. rows), then you can use find on the logical indices:
>> find(idz)
ans =
1 5
But keep in mind that logical indices are the fastest way to index (all of idx, idy and idz are logical indices).
And your questions:
1. This line creates one string containing all of the desired integers that you want to search for, separated by the vertical bar character. The vertical bar has a special meaning in regular expressions: it means "or". So this string tells regexp to match the first integer, or the second, or the third, etc. For my example data this string looks like this:
>> tmp
tmp = |1111|4444
2. This line
idy = ~cellfun('isempty',regexp(C,sprintf('^(%s)',tmp(2:end)),'once'));
creates the regular expression dynamically using sprintf. when you take out the sprintf you can see it is basically the same as your code:
idy = ~cellfun('isempty',regexp(C,...,'once'));
The sprintf creates the correct regular expression to detect the integer:
>> sprintf('^(%s)',tmp(2:end))
ans = ^(1111|4444)
The ^ matches the start of the line, the parentheses are required to group the or parts together. Read the regular expression documentation to learn more.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by