Change text to numbers in a cell
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
madhan ravi
am 7 Mai 2020
Bearbeitet: madhan ravi
am 7 Mai 2020
V = str2double(string(A)); % <2016a
V(isnan(V)) = 1
4 Kommentare
Mike Mierlo van
am 8 Mai 2020
Walter Roberson
am 8 Mai 2020
Does your actual code also have numeric values somewhere inside A ?
madhan ravi
am 8 Mai 2020
Dude when did you edit your question? For sure you edited it after I answered!
Mike Mierlo van
am 11 Mai 2020
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!