I have a cell array of strings with the following pattern
String Label
'Abc\a1\L\XYZ1R08' 1
'Abc\a1\R\XYZ1R09' 1
'Abc\a1\R\XYZ1R10' 1
'Abc\b2\L\XYZ2L01' 2
'Abc\b2\R\XYZ2L02' 2
'Abc\b2\R\XYZ2L03' 2
'Abc\c3\L\XYZ2L04' 3
'Abc\c3\R\XYZ2L05' 3
'Abc\d4\L\XYZ2L06' 4
'Abc\d4\R\XYZ2L07' 4
i wanted to give a new variable, "Label', values according to a1,b2,c3,d4, etc

댓글 수: 3

I'm not sure i understood what you wanted but here's my try:
elements = strsplit(my_string,'\');
if strcmp(elements(2),'a1')
Label = 1337;
end
Rik
Rik 2019년 11월 20일
Is that label guaranteed to be between the first two slashes?
Elysi Cochin
Elysi Cochin 2019년 11월 20일
편집: Elysi Cochin 2019년 11월 20일
yes sir, based on the first two slashes
find the different elements, and give same value for same elements
(a1, b2, c3, d4 - the names can change. This is just an example)
in my example i have 4 different values a1, b2, c3, d4 (it can be greater than 4 also)
So where all i have a1, i want to give 1 , for next value (b2) next value 2 and so on
% its a cell array
my_string = {
'Abc\a1\L\XYZ1R08';
'Abc\a1\R\XYZ1R09';
'Abc\a1\R\XYZ1R10';
'Abc\b2\L\XYZ2L01';
'Abc\b2\R\XYZ2L02';
'Abc\b2\R\XYZ2L03';
'Abc\c3\L\XYZ2L04';
'Abc\c3\R\XYZ2L05';
'Abc\d4\L\XYZ2L06';
'Abc\d4\R\XYZ2L07'};

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

 채택된 답변

Star Strider
Star Strider 2019년 11월 20일

2 개 추천

Try this:
String = {'Abc\a1\L\XYZ1R08'
'Abc\a1\R\XYZ1R09'
'Abc\a1\R\XYZ1R10'
'Abc\b2\L\XYZ2L01'
'Abc\b2\R\XYZ2L02'
'Abc\b2\R\XYZ2L03'
'Abc\c3\L\XYZ2L04'
'Abc\c3\R\XYZ2L05'
'Abc\d4\L\XYZ2L06'
'Abc\d4\R\XYZ2L07'};
grpstr = regexp(String, '(?<=\\)\w{2}', 'once','match'); % Get Two Letters After First ‘\’
Group = findgroups(grpstr);
Result = {String, Group} % Cell Array
T1 = table(string(String), Group) % Table
producing:
T1 =
10×2 table
Var1 Group
__________________ _____
"Abc\a1\L\XYZ1R08" 1
"Abc\a1\R\XYZ1R09" 1
"Abc\a1\R\XYZ1R10" 1
"Abc\b2\L\XYZ2L01" 2
"Abc\b2\R\XYZ2L02" 2
"Abc\b2\R\XYZ2L03" 2
"Abc\c3\L\XYZ2L04" 3
"Abc\c3\R\XYZ2L05" 3
"Abc\d4\L\XYZ2L06" 4
"Abc\d4\R\XYZ2L07" 4

댓글 수: 5

Elysi Cochin
Elysi Cochin 2019년 11월 20일
편집: Elysi Cochin 2019년 11월 20일
it works if string length is 2
but what if the string length between the first 2 slashes not unique
some cases 2 some cases 3
String = {'Abc\a1\L\XYZ1R08'
'Abc\a1\R\XYZ1R09'
'Abc\a1\R\XYZ1R10'
'Abc\bb2\L\XYZ2L01'
'Abc\bb2\R\XYZ2L02'
'Abc\bb2\R\XYZ2L03'
'Abc\c3\L\XYZ2L04'
'Abc\c3\R\XYZ2L05'
'Abc\da4\L\XYZ2L06'
'Abc\da4\R\XYZ2L07'};
what to do if the string between first 2 slashes is as above
Philippe Lebel
Philippe Lebel 2019년 11월 20일
편집: Philippe Lebel 2019년 11월 20일
replace
grpstr = regexp(String, '(?<=\\)\w{2}', 'once','match'); % Get Two Letters After First ‘\’
by
grpstr = regexp(String, '(?<=\\)\w*', 'once','match'); % Letters After First ‘\’
One small change in the regexp arguments is all that is needed:
grpstr = regexp(String, '(?<=\\)\w+', 'once','match'); % Get Letters After First ‘\’
Group = findgroups(grpstr)
T2 = table(string(String), Group) % Table
produciing for this version of ‘String’:
T2 =
10×2 table
Var1 Group
___________________ _____
"Abc\a1\L\XYZ1R08" 1
"Abc\a1\R\XYZ1R09" 1
"Abc\a1\R\XYZ1R10" 1
"Abc\bb2\L\XYZ2L01" 2
"Abc\bb2\R\XYZ2L02" 2
"Abc\bb2\R\XYZ2L03" 2
"Abc\c3\L\XYZ2L04" 3
"Abc\c3\R\XYZ2L05" 3
"Abc\da4\L\XYZ2L06" 4
"Abc\da4\R\XYZ2L07" 4
Note that ‘grpstr’ here is:
grpstr =
10×1 cell array
{'a1' }
{'a1' }
{'a1' }
{'bb2'}
{'bb2'}
{'bb2'}
{'c3' }
{'c3' }
{'da4'}
{'da4'}
So with that change, it is robust to changes in the string length between the first two ‘\’.
Elysi Cochin
Elysi Cochin 2019년 11월 20일
Star Strider
Star Strider 2019년 11월 20일
As always, my pleasure! (And our pleasure!)

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

추가 답변 (0개)

카테고리

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

태그

질문:

2019년 11월 20일

댓글:

2019년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by