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

2 Comments

Fangjun Jiang
Fangjun Jiang on 13 Feb 2018
What format do you need to "store", text file? sprintf('%.1f',50) outputs '50.0'. What is the issue?
Jergus Frajt
Jergus Frajt on 13 Feb 2018
e.g. desired output
somejson file...{ "numbers":[0.0, 0.0, 60.0]}...
those values got to be numeric because in the next block they are used, the "old" json file was written by hand, but I want to make it autogenerated

Sign in to comment.

 Accepted Answer

Akira Agata
Akira Agata on 14 Feb 2018
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]'

1 Comment

Jergus Frajt
Jergus Frajt on 15 Feb 2018
Thank you very much, regular expression is maybe the only thing I did not came up with :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Feb 2018

0 votes

json files are text files, so if you write your own, using fprintf(), then yes you can do that.

7 Comments

Jergus Frajt
Jergus Frajt on 13 Feb 2018
I am loading data from excel and then creating struct data. Then I use jsonencode matlab function. As mentioned above I need those values as numeric not strings.
James Tursa
James Tursa on 13 Feb 2018
The value 0 is the same as the value 0.0 in MATLAB ... they are both numeric double values with exactly the same internal bit pattern (all 0 bits). So we still don't know what your real issue is. An exact 0 does not "display" the .0 part by default, but it is still the same numeric value as 0.0
Image Analyst
Image Analyst on 13 Feb 2018
Why do you even care how it's listed in the json file? What does it matter?
Jergus Frajt
Jergus Frajt on 13 Feb 2018
I am sorry, maybe I wasn't precise in explaining. I of course understand that 0 is same value as 0.0 in MATLAB, but json file, I am trying to generate, is processed by other languages and I was told this desired output ( 0.0 ). So when I run of "googling" options, I made this question just to ask if it is possible. I hope I've made myself clear now :)
Image Analyst
Image Analyst on 13 Feb 2018
Do your other languages throw an error if it's 0 instead of 0.0?
James Tursa
James Tursa on 13 Feb 2018
No, not really. Numerically 0 and 0.0 are the same, so we assume you mean to write a text file with the output having 0.0 instead of 0. Others have already shown you how to do this with e.g. sprintf( ). Why doesn't this work for you?
Jergus Frajt
Jergus Frajt on 15 Feb 2018
Image Analyst, I don't know, I don't have access to next program chain blocks, I just have this desired output. James Tursa, I've tried sprintf(), but after calling jsonencode() function, it creates some unexpected ASCII characters. Thank you all, but it seems answer from Akira Agata worked for me :)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!