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];

1 件のコメント

Jan
Jan 2015 年 5 月 17 日
Where is the 4th element '4'?

サインインしてコメントする。

回答 (2 件)

Geoff Hayes
Geoff Hayes 2015 年 5 月 16 日

0 投票

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
Andrei Bobrov 2015 年 5 月 16 日

0 投票

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 ExchangeCharacters and Strings についてさらに検索

質問済み:

2015 年 5 月 14 日

コメント済み:

Jan
2015 年 5 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by