title: Why is my byte array returned from fread smaller than the number of bytes in the file itself when it includes unicode special symbols?
body: <p>I am having some trouble with encoding special characters and parsing XML in a big complex project in Matlab. I have isolated the problem and though I still don't know how to solve it, I think it is related to this question.</p>
  <p>Consider the following XML file:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;info&gt;
      &lt;desc&gt;
          &lt;channels&gt;
              &lt;channel&gt;
                  &lt;label&gt;accZ&lt;/label&gt;
                  &lt;unit&gt;mg₀&lt;/unit&gt;
                  &lt;type&gt;ACC&lt;/type&gt;
              &lt;/channel&gt;
          &lt;/channels&gt;
      &lt;/desc&gt;
  &lt;/info&gt;
  </code></pre>
  <p>Assuming Unix style line endings, this file contains 175 bytes of data. Indeed that is what it says when I open it in Notepad++. Now I have an XML parsing function that I copied almost exactly from Mathworks' explanation of how to parse XML in Matlab (<a href="https://au.mathworks.com/help/matlab/ref/xmlread.html" rel="nofollow noreferrer">https://au.mathworks.com/help/matlab/ref/xmlread.html</a>). This function works very well and is not the issue, it is included only for completeness' sake:</p>
  <pre><code>% parse a simplified (attribute-free) subset of XML into a MATLAB struct
  function result = parse_xml_struct(str)
  import org.xml.sax.InputSource
  import javax.xml.parsers.*
  import java.io.*
  tmp = InputSource();
  tmp.setCharacterStream(StringReader(str));
  result = parseChildNodes(xmlread(tmp));
  
  % this is part of xml2struct (slightly simplified)
      function [children,ptext] = parseChildNodes(theNode)
          % Recurse over node children.
          children = struct;
          ptext = [];
          if theNode.hasChildNodes
              childNodes = theNode.getChildNodes;
              numChildNodes = childNodes.getLength;
              for count = 1:numChildNodes
                  theChild = childNodes.item(count-1);
                  [text,name,childs] = getNodeData(theChild);
                  if (~strcmp(name,'#text') &amp;&amp; ~strcmp(name,'#comment'))
                      if (isfield(children,name))
                          if (~iscell(children.(name)))
                              children.(name) = {children.(name)}; end
                          index = length(children.(name))+1;
                          children.(name){index} = childs;
                          if(~isempty(text))
                              children.(name){index} = text; end
                      else
                          children.(name) = childs;
                          if(~isempty(text))
                              children.(name) = text; end
                      end
                  elseif (strcmp(name,'#text'))
                      if (~isempty(regexprep(text,'[\s]*','')))
                          if (isempty(ptext))
                              ptext = text;
                          else
                              ptext = [ptext text];
                          end
                      end
                  end
              end
          end
      end
  
  % this is part of xml2struct (slightly simplified)
      function [text,name,childs] = getNodeData(theNode)
          % Create structure of node info.
          name = char(theNode.getNodeName);
          if ~isvarname(name)
              name = regexprep(name,'[-]','_dash_');
              name = regexprep(name,'[:]','_colon_');
              name = regexprep(name,'[.]','_dot_');
          end
          [childs,text] = parseChildNodes(theNode);
          if (isempty(fieldnames(childs)))
              try
                  text = char(theNode.getData);
              catch
              end
          end
      end
  end
  </code></pre>
  <p>Now to test it:</p>
  <pre><code>finfo = dir('xml_example');
  sz = finfo.bytes
  fid = fopen('xml_example', 'r', 'ieee-le.l64');
  data = fread(fid, sz, '*char');
  data_size = size(data)
  h = parse_xml_struct(data);
  unit = h.info.desc.channels.channel.unit
  </code></pre>
  <p>and the output:</p>
  <pre><code>sz =
  
     175
  
  
  data_size =
  
     173     1
  
  
  unit =
  
      'mg₀'
  </code></pre>
  <p>So somehow I end up with the right output, but lost 2 bytes along the way. I do not understand why this is happening.</p>
  <p>And just to prove to myself that it is the little subscript 'o' that is causing the discrepancy between the size of the file and the number of bytes in my <code>data</code> array, I delete it from the XML file and get the following:</p>
  <pre><code>sz =
  
     172
  
  
  data_size =
  
     172     1
  
  
  unit =
  
      'mg'
  </code></pre>
  <p>Still the right output for the xml label and now the file size and the byte array size match. What's the deal?</p>
  <p><strong>Update</strong></p>
  <p>Furthermore, if I run the same test on a symbol that is 2-bytes long, I still get the compression phenomenon.</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;info&gt;
      &lt;desc&gt;
          &lt;channels&gt;
              &lt;channel&gt;
                  &lt;label&gt;ωX&lt;/label&gt;
                  &lt;unit&gt;mrad/s&lt;/unit&gt;
                  &lt;type&gt;AUX&lt;/type&gt;
              &lt;/channel&gt;
          &lt;/channels&gt;
      &lt;/desc&gt;
  &lt;/info&gt;
  </code></pre>
  <p>Output:</p>
  <pre><code>sz =
  
     175
  
  
  data_size =
  
     174     1
  
  
  unit =
  
  'ωX'
  </code></pre>