Change text to numbers in a cell
7 次查看(过去 30 天)
显示 更早的评论
Hi all,
Lets say I have 4 x 2 cell matrix A:
1 'text one' 'text two'
2 '5' 'text two'
3 'text one' 'text two'
4 '10' 'text two'
I want the result of a 4 x 2 cell matrix A of all doubles, exept for the last column. The 'text one' has to be replaced by 1 (double):
1 1 'text two'
2 5 'text two'
3 1 'text two'
4 10 'text two'
When I use a for-loop and if-statement I get the error that matrix dimensions must agree. Below is my faulty code:
for i = 1:size(A,1)
if A{i,2} == 'text one'
A{i,2} == 1
else
A{i,2} == str2double(A{i,2})
end
end
0 个评论
采纳的回答
Akira Agata
2020-5-8
How about the following?
B = replace(A,'text one','1');
B = cellfun(@str2double,B(:,1:end-1),'UniformOutput',false);
A = [B,A(:,end)];
3 个评论
Akira Agata
2020-5-8
Could you upload your original cell arrya A?
I have assumed as follows and my code works agains it.
A = {...
'text one', 'text two';
'5', 'text two';
'text one', 'text two';
'10', 'text two'};
更多回答(1 个)
madhan ravi
2020-5-7
编辑:madhan ravi
2020-5-7
V = str2double(string(A)); % <2016a
V(isnan(V)) = 1
4 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!