converting cell string into number
이전 댓글 표시
hello every one; how to convert string in cell into number where each character positioned in one column except the element position 18 or value 24.
example:
wm={'000' '402' '101' '000' '000' '0124' '011'};
estimated result:
y=[0;0;0;0;0;2;1;0;1;0;0;0;0;0;0;0;1;24;o;1;1];
답변 (2개)
Geoff Hayes
2015년 5월 16일
abdullahi - you can try the following code which iterates over each element in the cell array and splits each string into single digits. When the 18th is encountered, then the remaining portion of that string is used (this would be the 24).
y = [];
pos = 1;
% iterate over each string in the cell array
for k=1:length(wm)
% grab the string
str = wm{k};
% iterate over each character in the string
for v=1:length(str)
% if not at the 18th position then extract the single digit
if pos ~= 18
y = [y ; str2double(str(v))];
pos = pos + 1;
% else at the 18th position so extract the remaining digits of string
else
y = [y ; str2double(str(v:end))];
pos = pos + 1;
break;
end
end
end
Try the above and see what happens!
Andrei Bobrov
2015년 5월 16일
wm={'000' '402' '101' '000' '000' '0124' '011'};
z = cellfun(@(x){x(1),x(2),x(3:end)},wm,'un',0);
y = str2double([z{:}]);
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!