Changing output of text string via strrep

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 commentaires

Stephen23
Stephen23 le 25 Juin 2017
Modifié(e) : Stephen23 le 25 Juin 2017
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.
What are you actually trying to achieve? (because http://xyproblem.info/ seems likely).
Mark Kamps
Mark Kamps le 25 Juin 2017
Well, I use MATLAB in this case to process data obtained from an external program. I want to (automatically) read and save thousands of files into arrays. This reading is done by specifying the paths to the files, which contain a time stamp. The program saves these files with the timestep in the format described above. It is therefore not my choice to depict it like this.
The example I specified above is only a minimal working example, the actual times are loaded into a double loop. In your example the input for S is an array, which is not actually the case. I just tried the code and it works like a charm!
Thanks for your help!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Question posée :

le 25 Juin 2017

Commenté :

le 25 Juin 2017

Community Treasure Hunt

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

Start Hunting!

Translated by