Appending strings to array

243 visualizaciones (últimos 30 días)
wim
wim el 18 de Mzo. de 2021
Comentada: wim el 14 de Abr. de 2021
I would like to append a each word character of my string to an array, using the following code:
clear all
str = 'abc';
l = zeros(1,length(str)); % defining my empty list
for k = 1:length(str) % looping over the length of the string
l(k) = str(k);
end
The output I get for my list l however looks as like:
l =
97 98 99
I know that it is possible to get the individual string characters using l = num2str(str). However, I still can't figure out why this doesn't work.
  1 comentario
Stephen23
Stephen23 el 18 de Mzo. de 2021
Editada: Stephen23 el 18 de Mzo. de 2021
"I know that it is possible to get the individual string characters using l = num2str(str)"
To be honest, I don't see how this operation (which does absolutely nothing at all) is useful for you:
str = 'abc';
out = num2str(str)
out = 'abc'
It returns exactly the same character vector. If you want to "get the individual" characters (whatever that means) then you can do that just as well with the original (completely identical) character vector.

Iniciar sesión para comentar.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 18 de Mzo. de 2021
The problem is likely due to a mismatch in data types. str contains chars, but l is defined as doubles. So when you assign a char to a double, you get the ascii code for the char - it's value as a double.
Perhaps you want to extract each letter?
str = 'abc';
l = strings(1,length(str)); % defining my empty list
for k = 1:length(str) % looping over the length of the string
l(k) = str(k);
end
l
l = 1×3 string array
"a" "b" "c"
  2 comentarios
Stephen23
Stephen23 el 18 de Mzo. de 2021
Editada: Stephen23 el 18 de Mzo. de 2021
I don't see how that "extracts each letter", it actally just hides the individual, actual characters inside what is essentially a container array class. For someone wanting to perform actual character code manipulations (which quite possibly this OP does not, but some people do) this just makes it harder to access the data.
In any case:
str = 'abc';
out = string(str(:))
out = 3×1 string array
"a" "b" "c"
Or:
out = string(num2cell(str))
out = 1×3 string array
"a" "b" "c"
wim
wim el 14 de Abr. de 2021
Thank you for all the suggestions. What Cris suggested is most similar to what I intended.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 18 de Mzo. de 2021
Editada: Stephen23 el 18 de Mzo. de 2021
"l = zeros(1,length(str)); % defining my empty list "
That is not an "empty list":
  • MATLAB does not have a "list" type.
  • It is not empty.
  • It is actually a numeric array with size 1x3, filled with zeros.
When you allocate characters to a numeric array MATLAB simply allocates the character code to the array. Note that MATLAB arrays are homogenous, that is their elements must be all of the same class. This means you cannot store characters with type char in a numeric array: all elements of a numeric array are numeric.
If you want to store different classes of data in one array (e.g. numeric and char) then you will need to use some kind of container array (e.g. a cell array, a structure, a table, etc.).
If you want to store characters in an array, then of course you can use a character array, e.g.:
str = 'abc';
out = repmat(' ',size(str));
for k = 1:numel(str)
out(k) = str(k);
end
out
out = 'abc'
Or the MATLAB approach:
out = str
out = 'abc'

Categorías

Más información sobre Logical 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