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

Risposte (2)

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!
wm={'000' '402' '101' '000' '000' '0124' '011'};
z = cellfun(@(x){x(1),x(2),x(3:end)},wm,'un',0);
y = str2double([z{:}]);

Categorie

Commentato:

Jan
il 17 Mag 2015

Community Treasure Hunt

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

Start Hunting!

Translated by