How to change number precision with writestruct

The number of significant digits of writestruct is quite limited, as showed by this example
s=struct('pi',pi);
writestruct(s,'pi.xml');
type pi.xml
<?xml version="1.0" encoding="UTF-8"?> <struct> <pi>3.1416</pi> </struct>
Does anyone know how to change/control/increase it?

 Accepted Answer

s=struct('pi', num2str(pi, 15)); % pi with 15 significant figures
writestruct(s, 'pi.xml');
type pi.xml
Output:
<?xml version="1.0" encoding="UTF-8"?>
<struct>
<pi>3.141592653589793</pi>
</struct>

4 Comments

I knew this workaround trick.
However not applicable in real life. For example more complicated example
piseries = pi + (0:10);
s=struct('piseries', piseries);
writestruct(s,'pi.xml');
s=readstruct('pi.xml');
s.piseries+1
ans = 1×11
4.1416 5.1416 6.1416 7.1416 8.1416 9.1416 10.1416 11.1416 12.1416 13.1416 14.1416
s=struct('piseries', num2str(piseries,15));
writestruct(s,'pi.xml');
s=readstruct('pi.xml');
s.piseries+1
ans = "3.14159265358979 4.14159265358979 5.14159265358979 6.14159265358979 7.14159265358979 8.14159265358979 9.14159265358979 10.1415926535898 11.1415926535898 12.1415926535898 13.14159265358981"
OK, I get it.
But, what about a workaround to the workaround?
Such as changing
s.piseries+1
to
str2double(split(s.piseries))+1
ans =
4.14159265358979
5.14159265358979
6.14159265358979
7.14159265358979
8.14159265358979
9.14159265358979
10.1415926535898
11.1415926535898
12.1415926535898
13.1415926535898
14.1415926535898
Thanks for the effort Scott.
To me still not ideal. This XML is used as interface with other SW (otherwise I won't bother with XML). It a pain to minimze the precision output.
Yes, I see your point. I studied the documentation and clearly writestruct is not intended for numeric variables. The focus is on text, and xml in particular. Good luck.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!