Converting all texts to numbers in a cell that has numbers and texts

3 visualizaciones (últimos 30 días)
Yaser Khojah
Yaser Khojah el 14 de Feb. de 2021
Comentada: Yaser Khojah el 14 de Feb. de 2021
Hello there, I have a structure (X) that contains text and numbers and I would like to convert them to a vector with numbers only. So, I just wanted any text to be written as zero instead of a text as '0'. I'm using the code below. Anyone can help please.
% my cell is F with size 58X11 Cell
% It looks as below (e.g.)
% F = ['0' 1 '0' 3'
% 4 '0' 34 10]
% I would like to make looks like the below
% F = [0 1 0 3
% 4 0 34 10]
% my code
for i = 1:L
E = C{1,i} % the structure contains text and numbers. The texts are written as '0', which I wnat to conver to number 0
F = E(9:66,2:12);
X = str2double(F); % here X is double but the '0' became 0, while the numbers became Nan
end

Respuestas (1)

Walter Roberson
Walter Roberson el 14 de Feb. de 2021
X = zeros(size(F));
mask = cellfun(@ischar, F);
X(~mask) = cell2mat(F(~mask)); %copy non-text directly
X(mask) = str2double(F(mask)); %convert the text

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by