how to extract n characters from a string ???

I have a dataset including a variable named "code". I format it as type "character".
This variable contains such observations like G4411, H5551, K9878..
Now I want to extract the first character out of every observation.
This means G4411 --> G, H5551 --> H, K9878 --> K
I have also attached the data file at the end of the post!
Could somebody help me with this problem?
Many Thank and best regard!

 Respuesta aceptada

Star Strider
Star Strider el 8 de Mayo de 2016
I can get it to work with your example but not your data:
C = {'G4411'; 'H5551'; 'K9878'};
res = regexp(C, '\w','match','once')
res =
'G'
'H'
'K'
When I try to load your data, it crashes my computer.

5 comentarios

Weird Rando
Weird Rando el 8 de Mayo de 2016
Editada: Weird Rando el 8 de Mayo de 2016
additional code:
load matlab.mat
C = dataset2cell(data(:,4));
Then Star's code with out the first line of code.
Thank you, Scott. Tired tonight.
C = dataset2cell(data(2:end,4));
would definitely work. Thanks for reminding me about dataset2cell.
Dung Le
Dung Le el 8 de Mayo de 2016
Thanks :) It works very well !
Star Strider
Star Strider el 8 de Mayo de 2016
My (our) pleasure!
Weird Rando
Weird Rando el 8 de Mayo de 2016
:)

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 8 de Mayo de 2016

Comentada:

el 8 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by