Simplest way to extract multiple tokens from multiple strings and store each type of token in it's own cell array?

조회 수: 5 (최근 30일)
So lets say I have:
str1="something function abc (a, b, c)"
str2="something function def (d, e, f)"
str3="something else function ghi (g, h, i)"
strarray=[str1;str2;str3]
and I want to end up with the following two string arrays:
functions={"abd"
"def"
"ghi"}
arguments={"a, b, c"
"d, e, f"
"g, h, i"}
What is the simplest way to accomplish this? The simplest thing I've come up with so far is this:
[alltok]=regexp(strarray,'function (\w*) \(([^\)]*)','tokens','once')
alltok2=[alltok{:}]'
functions=alltok2(1:2:end)
arguments=alltok2(2:2:end)
Is there a way to reduce this to 2 lines or even 1 line? If it would help to store the strings as something other than a string array then that's not a problem.
EDIT:
I just figured out how to get it down to two lines:
functions=string(regexp(strarray,'function (\w*)','tokens'))
arguments=string(regexp(strarray,'function \w* \(([^\)]*)','tokens'))
or one big ugly line:
[functions arguments]=deal(string(regexp(strarray,'function (\w*)','tokens')),string(regexp(strarray,'function \w* \(([^\)]*)','tokens')))
Is it time to leave good enough alone or is there a more intuitive way of doing this? In the first option I posted, regexp outputs a 3x1 cell array of 1x2 string arrays. I feel like there should be some simple way to unpack that into two separate string arrays.

채택된 답변

woahs
woahs 2020년 1월 21일
Not entirely sure what you're asking for since it sounds like you were able to achieve what you wanted but here's a way to do it with fewer lines of code, albiet possibly more confusing:
[alltok]=regexp(strarray,'function (\w*) \(([^\)]*)','tokens','once')
[functions, arguments] = cellfun(@(x) deal(x{1}, x{2}), alltok, 'UniformOutput', false)
  댓글 수: 1
Philip M
Philip M 2020년 1월 21일
편집: Philip M 2020년 1월 21일
Thanks, this is helpful. This also answers my underlying question of how to unpack a cell array of string arrays. For this specific operation I think my two-line option is perferable, but if I ever have lots of tokens then your code would be better. I hadn't considered using deal in conjunction with cellfun, I'm sure I'll use that in the future.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by