Miraculous behavior of readstruct() when importing Attributes with a single d or e

7 visualizzazioni (ultimi 30 giorni)
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 Commenti
Eleftherios Bethmage
Eleftherios Bethmage il 19 Mar 2022
Modificato: Eleftherios Bethmage il 19 Mar 2022
@Les Beckham do you know of any way to add the 0x to each value? Imagine my XML would cover 10000 files. It would be impossible to do it by hand. Problem is that as soon as I import into matlab the information is already lost. So I must alter the file without loading it into the workspace right? Is this even possible?
Also if I use dec2hex it does not perserve any leading 0, which could be fixed easily but still is a bit unfortunate.
EDIT: fileread() and regexprep() did the trick for me. It is weird that I must go that route but it is a solution.
@Walter Roberson thank you for the explaination with d and e. I was wondering since the XML contains "123e45" with quotation marks and matlab still treats it as a number...
AndresVar
AndresVar il 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

Accedi per commentare.

Risposte (1)

Les Beckham
Les Beckham il 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
ans = 8×1
1869801 965507866 13639940 221849700 1083529448 1379479159 8208420 1466798993
class(T.CRC32Attribute)
ans = 'uint32'
T.CRC32Attribute = dec2hex(T.CRC32Attribute, 8)
T = 8×4 table
nameAttribute CRC32Attribute MD5Attribute SHA1Attribute _____________ ______________ __________________________________ __________________________________________ "Test1.txt" 001C87E9 "khcme9u7c3hgjvdrctx3u2u9l0ebj7qj" "nxnh6ywylck2rnplyl8isksn73kr8beut41jmgtv" "Test2.txt" 398C7B1A "n8le51jc8elgqcczr9x6fjqvk7b243a5" "f8u0x6ftsgskel1cohdgwwz1qjb3vkmznno05ldb" "Test3.txt" 00D02104 "wx39m75pw573aj12qkwaqsc84vjsni3f" "c9svilvjwlwyo1s2k5yrh19s41z33lhc0h2wpf6o" "Test4.txt" 0D392864 "hoa7484gbb5b9fxhirbh56mo9x4awufj" "nnhkvy31xprai5slctk1q4urjbpx8p21lqsi7l4e" "Test5.txt" 409558E8 "slojwfowq7lqueoq35qpituuc206m050" "rcb0kulbclkfqjq7qk6yrem6zsyzvu6gnt6lh0at" "Test6.txt" 52392E77 "xbzxu2tphoa9zh1uk553w4xuoztln04g" "t5930b1282k094hr618pmep7hriq9n2ks2xk1fo1" "Test7.txt" 007D4024 "tzgh3ljocpvzq4rc7d1f2hfow9tg21hf" "ke4c394hr6182a2d5b7b778ke4c38ke4c30fke3p" "Test8.txt" 576D9391 "shi8gna8ll2vxm2fnt3g08c4yloa503s" "b3d65fhhes3e4b32fzzqlke4c30f61b3dchaz526"
class(T.CRC32Attribute)
ans = 'char'

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by