How to read an array of set number of characters from a binary file while skipping bytes in between.
Afficher commentaires plus anciens
I'm trying to read a binary file, and I'm wondering if there is a better way to read the characters. A portion of the binary file repeats the same sequence, x number of times
32 characters, integer, integer, integer, integer
If I had just 32 character repeated x times, I can use:
names = convertCharsToStrings(fread(fileID, [ 32 x], '*char'));
I would like to do something like this instead since there are bytes in between:
names = convertCharsToStrings(fread(fileID, [ 32 x], '*char'), 32 + 8*4);
That it not working the way that I hoped. I assume it is reading one byte, then skipping 32 + 8*4 before reading the next byte.
I have a workaround where I read the first character of each of the x sequences, then read the 2nd character, and so on.
names( 1:x, 1:32) = ' ';
for i = 1:32
names(:, i) = convertCharsToStrings(fread(fileID, [ 1 x ], '*char', 31 + 8 *4));
fseek(fileID, -8*(x*4)-32*x + 1, 0);
end
This accomplishes what I need, but is there an easier, better, or faster way to do this?
1 commentaire
Walter Roberson
le 9 Déc 2024
You can reduce the load a bit if you read in a uint64 and typecast it to uint8 and char() that. You would only need to loop 4 times instead of 32 and the I/O would be more efficient.
Note that you might need to fopen() with 'ieee-be' to get the right byte order when you do the above.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!