Converting all texts to numbers in a cell that has numbers and texts
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Walter Roberson
on 14 Feb 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
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!