Changing output of text string via strrep
Ältere Kommentare anzeigen
Dear community,
I am currently experiencing some problems with some of my code. I cant figure out how to get this to work.. Suppose I have a text string containing numbers which will be used in a larger text string to specify a path:
Time_orignal = num2str(TIME,'%.2f')
Time_short = strrep(Time_orignal, '0.', '.');
Where the value for time would be a number which increases: 0.0100 0.0200 ... 9.9900 10.0000 10.0100 etc.
The above mentioned code works the way I want to: only 2 digits after the decimal point, and remove a starting zero (so 0.0200 becomes .02). However, when the value for TIME goes into the double digits, it will remove the '0' from '10', which is not what I want (it will change 10.0100 to 1.01).
How can I prevent the removal of this zero, while it should remove a starting zero?
Mark
2 Kommentare
To remove a leading zero you could try a regular expression:
regexprep(S,'(^| )0.','$1.')
But you will likely run into other problems, as num2str simply runs all of the values together, which is unlikely to be useful for much:
>> S = num2str(9.9:0.01:10.1,'%.2f')
S =
9.90 9.91 9.92 9.93 9.94 9.95 9.96 9.97 9.98 9.9910.0010.0110.0210.0310.0410.0510.0610.0710.0810.0910.10
>>
Every publishing style-guide and journal I have seen requires leading zeros if the values can be outside of the range [0,1]. It is not clear what possible advantage there is to removing the leading zero.
Mark Kamps
am 25 Jun. 2017
Antworten (0)
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!