using sprintf for floating point numbers
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
summyia qamar
am 16 Dez. 2016
Kommentiert: Image Analyst
am 16 Dez. 2016
optEnergyCost=[42 36 28.5 26 39.7 25 57047]
optEnergyCostString=sprintf('%g',optEnergyCost)
optEnergyCostString=strrep(optEnergyCostString,'42','Route_1');
optEnergyCostString=strrep(optEnergyCostString,'36','Route_1');
optEnergyCostString=strrep(optEnergyCostString,'28.5','Route_2');
optEnergyCostString=strrep(optEnergyCostString,'26','Route_2');
optEnergyCostString=strrep(optEnergyCostString,'39.7','Route_1');
optEnergyCostString=strrep(optEnergyCostString,'25','Route_1');
but results are
optEnergyCostString =
423628.52639.72557046.5 how to get this as optEnergyString= route1 route1 route2 route2 route1 route1 57047
3 Kommentare
Image Analyst
am 16 Dez. 2016
And I answered it there: http://www.mathworks.com/matlabcentral/answers/317037-replace-numbers-with-letters-in-a-vector-or-matrix#answer_247544 so your code will look normal.
By the way, for next time, read this http://www.mathworks.com/matlabcentral/answers/13205#answer_18099
Akzeptierte Antwort
James Tursa
am 16 Dez. 2016
Bearbeitet: James Tursa
am 16 Dez. 2016
You don't have a semi-colon after this line:
optEnergyCostString=sprintf('%g',optEnergyCost)
so this result gets displayed to the screen. Then the other lines do the strrep stuff, but you don't ever display those results. So the only thing that gets displayed to the screen is the result of that first line, fooling you into thinking that that is the final result of the code (which it isn't), even though all of the other string replacements actually took place. All you need to do is simply examine optEnergyCostString after the code gets run.
To get your desired result, you will need to add a trailing space to your replacement strings.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!