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

Jan
Jan 2016년 12월 16일
You are funny. How could we know what you find "correct". All we see is the code which does what the code does. But what do you want instead?
summyia qamar
summyia qamar 2016년 12월 16일
mentioned now in question

댓글을 달려면 로그인하십시오.

 채택된 답변

James Tursa
James Tursa 2016년 12월 16일
편집: James Tursa 2016년 12월 16일

0 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Desktop에 대해 자세히 알아보기

제품

태그

질문:

2016년 12월 16일

댓글:

2016년 12월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by