Miraculous behavior of readstruct() when importing Attributes with a single d or e
7 views (last 30 days)
Show older comments
Hi,
I have some question regarding readstruct(). I have a XML called test.xml which looks like this:
<folder>
<file name="Test1.txt" CRC32="001c87e9" MD5="khcme9u7c3hgjvdrctx3u2u9l0ebj7qj" SHA1="nxnh6ywylck2rnplyl8isksn73kr8beut41jmgtv"/>
<file name="Test2.txt" CRC32="398c7b1a" MD5="n8le51jc8elgqcczr9x6fjqvk7b243a5" SHA1="f8u0x6ftsgskel1cohdgwwz1qjb3vkmznno05ldb"/>
<file name="Test3.txt" CRC32="00d02104" MD5="wx39m75pw573aj12qkwaqsc84vjsni3f" SHA1="c9svilvjwlwyo1s2k5yrh19s41z33lhc0h2wpf6o"/>
<file name="Test4.txt" CRC32="0d392864" MD5="hoa7484gbb5b9fxhirbh56mo9x4awufj" SHA1="nnhkvy31xprai5slctk1q4urjbpx8p21lqsi7l4e"/>
<file name="Test5.txt" CRC32="409558e8" MD5="slojwfowq7lqueoq35qpituuc206m050" SHA1="rcb0kulbclkfqjq7qk6yrem6zsyzvu6gnt6lh0at"/>
<file name="Test6.txt" CRC32="52392e77" MD5="xbzxu2tphoa9zh1uk553w4xuoztln04g" SHA1="t5930b1282k094hr618pmep7hriq9n2ks2xk1fo1"/>
<file name="Test7.txt" CRC32="007d4024" MD5="tzgh3ljocpvzq4rc7d1f2hfow9tg21hf" SHA1="ke4c394hr6182a2d5b7b778ke4c38ke4c30fke3p"/>
<file name="Test8.txt" CRC32="576d9391" MD5="shi8gna8ll2vxm2fnt3g08c4yloa503s" SHA1="b3d65fhhes3e4b32fzzqlke4c30f61b3dchaz526"/>
</folder>
I want to import this as a struct into MATLAB and therefore run these two lines:
Test = readstruct("test.xml");
Test = Test.file;
What I end up with is a struct with 4 fields: nameAttribute, CRC32Attribute, MD5Attribute and SHA1Attribute, which is exaclty what I want.
But oddly there is a problem with CRC32Attribute:

I purposefully structured the test.xml into pairs to illustrate my problem:
- Test1.txt and Test2.txt have crcAttribute as wanted. It is identically to test.xml as expected and wanted
- Test3.txt and Test4.txt have crcAttribute as 0 which is not what I want.
- When looking at the XML data I noticed that it might relate to the fact that there is a leading 0 followed by a d and some other numbers with no other letters. Is d the opposite of e+10 and therefore sets the value to 0?
- Still I don't want a number I want the original string value not 0
- Test5.txt and Test6.txt have crcAttribute expanded by a power of 10 which is not what I want
- I think it relates to the previous point just that it is now e instead of d
- Test7.txt and Test8.txt have crcAttribute expanded by a power of 10 which is not what I want
- Here I have the d's again but why is it here Inf and not 0?
In a nutshell I want readstruct() to import the CRC32Attribute as it is in the test.xml as a string. No 0, no Inf or whatsoever.
Please note that I am aware that there are other ways to import this kind of data. I am just really curious how this specific problem is solved and what the cause is.
Thank you!
4 Comments
AndresVar
on 19 Mar 2022
@Eleftherios Bethmage also you could use find/replace/replace-all to add 0x. dec2hex(num,min) has a second argument to add leading zeros
Answers (1)
Les Beckham
on 19 Mar 2022
As @AndresVar says, just use a text editor to search and replace CRC32=" with CRC32="0x. Or, as you suggest you can do it with Matlab.
Also, the suggestion of using the second argument to dec2hex is a good one. For example:
% load the mat file containing table generated by using readtable on the
% modified xml file (added "0x" in front of the CRC32 attribute values)
% i.e., T = readtable('Test0x.xml')
% (since Answers doesn't allow xml attachments)
load Test0x.xml.mat
T.CRC32Attribute
class(T.CRC32Attribute)
T.CRC32Attribute = dec2hex(T.CRC32Attribute, 8)
class(T.CRC32Attribute)
See Also
Categories
Find more on Structures 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!