give labels according to string

6 visualizaciones (últimos 30 días)
Elysi Cochin
Elysi Cochin el 20 de Nov. de 2019
Comentada: Star Strider el 20 de Nov. de 2019
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 comentarios
Rik
Rik el 20 de Nov. de 2019
Is that label guaranteed to be between the first two slashes?
Elysi Cochin
Elysi Cochin el 20 de Nov. de 2019
Editada: Elysi Cochin el 20 de Nov. de 2019
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'};

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 20 de Nov. de 2019
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 comentarios
Elysi Cochin
Elysi Cochin el 20 de Nov. de 2019
Star Strider
Star Strider el 20 de Nov. de 2019
As always, my pleasure! (And our pleasure!)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by