Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 6 de Jul. de 2011

2 votos

name = 'Microsoft'
out = name(1:6)

2 comentarios

Real User
Real User el 26 de Nov. de 2022
Editada: Real User el 26 de Nov. de 2022
What if name has < 6 characters?
Is there some short way or do have have to write
out = name(1:min(6,length(name)));
substr seems to require some stateflow package https://se.mathworks.com/help/stateflow/ref/substr.html
name = 'Microsoft';
name(1:min(end,6))
ans = 'Micros'
name = 'Cat';
name(1:min(end,6))
ans = 'Cat'

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 26 de Nov. de 2022
This wasn't an option when the question was originally asked, but the extractBefore function will work for both char vectors and string arrays.
c = 'Microsoft'
c = 'Microsoft'
c6 = extractBefore(c, 7)
c6 = 'Micros'
s = string(c)
s = "Microsoft"
s6 = extractBefore(s, 7)
s6 = "Micros"

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Jul. de 2011

Respondida:

el 26 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by