Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have an HDF5 file with a fixed-length dataset containing fixed-length strings. Each string contains a null terminator somewhere in it, which other languages (e.g. Python, C, etc) understand marks the end of the string, and that characters beyond the null terminator are invalid and shouldn't be read out. But matlab's h5read function doesn't seem to understand this; it always gives me the entire string, including any junk characters that exist past the null-terminated part.

I can't seem to find an obvious function for removing elements after the null terminator, but it feels like this should exist. As an example, here's what one of my strings might look like:

['actual string' double(0) 'junk characters after null termination']

Is there a function that will trim off everything after the null terminator, or do I just need to do it myself? It should be easy to write, but I'd like to use a preexisting Matlab function if one exists. Something like this would work, but feels hacky/ugly:

x = x(1:find(x == 0, 1) - 1)
                Also, your solution would error I think if there are more than one null character. I would use something like x(find(x,1,'first'):end)=[].
– Cris Luengo
                Jun 21, 2019 at 0:55
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.