필터 지우기
필터 지우기

Appending strings to array

조회 수: 139 (최근 30일)
wim
wim 2021년 3월 18일
댓글: wim 2021년 4월 14일
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
Stephen23
Stephen23 2021년 3월 18일
편집: Stephen23 2021년 3월 18일
"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.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 3월 18일
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
Stephen23
Stephen23 2021년 3월 18일
편집: Stephen23 2021년 3월 18일
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 2021년 4월 14일
Thank you for all the suggestions. What Cris suggested is most similar to what I intended.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2021년 3월 18일
편집: Stephen23 2021년 3월 18일
"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'

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by