Error in converting array in double to string
이전 댓글 표시
These errors occured when I tried to convert an array in double to string using the following methods.
Method 1:
str = [];
arr = [1,2,3,4,5];
for i=1:length(arr)
str(end+1) = num2str(arr(i));
end
disp(str)
49 50 51 52 53
Method 2:
arr = [1,2,3,4,5];
str = zeros(1,length(arr))
for i=1:length(arr)
str(i) = sprintf('%0.0f',arr(i));
end
disp(str)
49 50 51 52 53
I hoped to get something like [ '1', '2', '3', '4', '5' ] but the answers were these numbers (49,50,51,52,53) which I don't know from where they are coming from.
I tried using only num2str(arr(1)) or sprintf('%0.0f', arr(1)) to test whether they are giving correct answers, which to my suprise returned '1' as a string in both the cases. The error is occuring only while appending the string to the array.
Can anyone please provide an explaination for this?
채택된 답변
추가 답변 (1개)
Malik Cheriaf
2021년 9월 7일
0 개 추천
str=string(arr)
% result=[ "1" "2" "3" "4" "5"]
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!