Can I pull a single character froma string?

62 visualizaciones (últimos 30 días)
Alex
Alex el 15 de Sept. de 2017
Respondida: James Tursa el 15 de Sept. de 2017
I'm trying to pull a single character from a user defined string.
A = "Hello, World";
B = "W";
I have A, I want to get B from it. I have tried
B = A(end-3);
as well as
B = A(8);
the former gave me the following error "Subscript indices must either be real positive integers or logicals," and the latter worked about as well as is expected.
  1 comentario
Stephen23
Stephen23 el 15 de Sept. de 2017
Editada: Stephen23 el 15 de Sept. de 2017
The whole point of strings is that they are atomic, and each string constitutes one whole element of a string array. So what you are doing is a bit contradictory to the concept of a string. Of course getting one character of a character array is trivially easy, so you could just convert the string to a char array and use indexing.

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
James Tursa el 15 de Sept. de 2017

Categorías

Más información sobre Characters and Strings 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