Nested Indexing in a Single Line command

Is it possible to extract different data in the following structured string using a single line command?
exp={'George: A5 == BB';...
'Anna: C3 == DD';...
'Smith: E2 == FFF';...
'Ken: G8 == HHHH'};
For example, the obvious method to extract HHHH is the following:
mystr1=split(exp(end),': ',2);
mystr2=split(mystr1(:,2),' == ',2);
mystr2(end)
Can you suggest a single line of command to extract HHHH? Please note all names, right-hand and left-hand sides of the equations can be anything with different lenghts or characters. Only : and == characters are unchanged. The code needs be be universal meaning it sould extract anything (index 3 and 1 for Smith or index 2 and 2 for C3 or index end and end for HHHH) only using a single line command.

댓글 수: 4

Bob Thompson
Bob Thompson 2019년 1월 14일
Why do you need to split at ':' and '=='? Splitting at just the equals should work fine.
Also, you can probably use regexp to split at both special characters.
mystr = regexp(exp{end},'\W','split');
mystr{end};
I have not tested this for specific syntax, but the concept should be sound.
S H
S H 2019년 1월 14일
편집: S H 2019년 1월 14일
Thank you Bob but I am wondering if it is possible to combine
mystr=regexp(....)
and
mystr{end}
into a single line command? Something similar to regexp(...){ind1,ind2}?
Bob Thompson
Bob Thompson 2019년 1월 14일
So, all you're looking to do is display the answer? This can be done by unsurpressing the regexp command (remove the semicolon at the end of the line).
S H
S H 2019년 1월 14일
Ok, I will try it. Thanks.

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

 채택된 답변

Steven Lord
Steven Lord 2019년 1월 14일

0 개 추천

Using the answer from Bob Nbob's comment:
mystr = regexp(exp{end},'\W','split'); mystr = mystr{end};
That's one line (albeit not one command.)
The type of indexing you asked about doesn't exist, at least not directly. You could write an explicit call to subsref to get that in one command, but in my opinion it would be much less clear than writing those two commands.Is there a particular reason why you need it in one command?
If you want to do this to use this in an anonymous function inside a script, and you're using release R2016b or later instead consider creating a local function inside your script file.

댓글 수: 1

S H
S H 2019년 1월 14일
I am trying to write a memory efficient prgram. It is very long. The code will not be efficient if for every calculation, a temporary variable should be created and then removed.
I think using a function helps me to achieve what I need. At least, functions have their own workspace that is removed by Matlab after using functions.
Thanks.

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

추가 답변 (1개)

Stephen23
Stephen23 2019년 1월 14일
편집: Stephen23 2019년 1월 14일

0 개 추천

>> C = {'George: A5 == BB'; 'Anna: C3 == DD'; 'Smith: E2 == FFF'; 'Ken: G8 == HHHH'};
>> D = regexp(C,'^(\w+):\s*(\w+)\s*==\s*(\w+)','tokens','once');
>> horzcat(D{:}) % or VERTCAT, depending on your needs.
ans =
[1,1] = George
[2,1] = A5
[3,1] = BB
[1,2] = Anna
[2,2] = C3
[3,2] = DD
[1,3] = Smith
[2,3] = E2
[3,3] = FFF
[1,4] = Ken
[2,4] = G8
[3,4] = HHHH

댓글 수: 4

S H
S H 2019년 1월 14일
편집: S H 2019년 1월 14일
Thank you Stephen but I am wondering if it is possible to combine
D=regexp(....)
and
horzcat(...)
into a single line command? Something similar to regexp(...){ind1,ind2}?
LastCell = @(C) C{end};
then
LastCell( regexp(C,'^(\w+):\s*(\w+)\s*==\s*(\w+)','tokens','once') )
S H
S H 2019년 1월 14일
I just want to show a data (for example 'HHH' or 'E2') at a time. Not all the data in a row.
C = {'George: A5 == BB'; 'Anna: C3 == DD'; 'Smith: E2 == FFF'; 'Ken: G8 == HHHH'};
disp(char(regexprep(C, '^.*==\s*', '')))

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

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

S H
2019년 1월 14일

댓글:

2019년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by