Appending strings to array
138 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 18 Mar 2021
Edited: Stephen23
on 18 Mar 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)
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.
Accepted Answer
Cris LaPierre
on 18 Mar 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
2 Comments
Stephen23
on 18 Mar 2021
Edited: Stephen23
on 18 Mar 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(:))
Or:
out = string(num2cell(str))
More Answers (1)
Stephen23
on 18 Mar 2021
Edited: Stephen23
on 18 Mar 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
Or the MATLAB approach:
out = str
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!