MATLAB: Store 0 as 0.0

decimalplacessave

Please is it possible to store 0 as 0.0(or 50 as 50.0? I have to save numeric values to json file ( using jsonencode ). I need this specific format due to dependency forward in program chain ( I cannot change the input format of the next block and it is specified that this format is needed). I've spent 4 hours browsing and trying but I am still stuck 🙁 (sprintf('%.1f',var) did not worked… Any help appreciated

Best Answer

How about using regexprep to add '.0' for integer values? Here is an example.
x = [0.0 12.0 3.1];
str = jsonencode(x);
output = regexprep(str,'(?<=([|,))[0-9]+(?=(,|]))','$0.0');
The output is:
>> output
output =
'[0.0,12.0,3.1]'