Modifying elements in a list
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jensen
am 25 Mär. 2019
Beantwortet: Star Strider
am 25 Mär. 2019
I am trying to change the type of the elements in my 1-dimensional matrix from string type to number type using str2double, and modifying the previous value at that position to have the number instead of the string. For some reason when I run my code it isnt working. How should I approach this to make it work? I need to have these values as numbers for future computations.
times = ["758" "808" "828" "838" "848" "858" "908"]
for num = 1 : length(times)
times(1,num) = str2double(times(1,num)) ;
end
0 Kommentare
Akzeptierte Antwort
Star Strider
am 25 Mär. 2019
You do not need the loop.
Just do this:
timestr = ["758" "808" "828" "838" "848" "858" "908"]
timenum = str2double(timestr)
Note that times (link) is actually a MATLAB function for doing element-wise multiplication. Naming variables to be the same as MATLAB functions is called ‘overshadowing’. The times function will not do what you intend if you call it after overshadowing it, so I changed your vector names to avoid that.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!