converting cell string into number
Mostrar comentarios más antiguos
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 comentario
Jan
el 17 de Mayo de 2015
Where is the 4th element '4'?
Respuestas (2)
Geoff Hayes
el 16 de Mayo de 2015
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
el 16 de Mayo de 2015
wm={'000' '402' '101' '000' '000' '0124' '011'};
z = cellfun(@(x){x(1),x(2),x(3:end)},wm,'un',0);
y = str2double([z{:}]);
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!