fread size mismatch in Code Generation

Hello!
I am trying to build .mex files from my code but the MATLAB Coder App fails at this line and other similar ones:
fixed_size_matrix(ii,jj) = fread(fileID, 1, '*uint8');
The error is:
Size mismatch ([1 x 1] ~= [:? x 1])
I guess this is because fread may return [] if the file end is reached. I tried to do a wrapper function which tests for an empty return value and then generates a zero vector of appropriate size and type. But of course code generation does not support dynamic typing.
What would be the appropriate solution here?

Réponses (2)

From an initial review of your code, I understand that you are trying to store extracted data into a fixed size matrix.
Here, in 'fread' function you are extracting 1-byte of data (1x1 size), and trying to store it in a fixed size matrix (more than 1x1 size), which is logically incorrect. Instead, you can store it as follows,
fixed_size_matrix(:,jj) = fread(fileID, 1, '*uint8');
or
fixed_size_matrix(ii,:) = fread(fileID, 1, '*uint8');
You can check fread documentation for more information.

1 commentaire

Walter Roberson
Walter Roberson le 29 Déc 2017
I disagree with the above. The user is trying to read exactly one byte and store it into exactly one location. However, it is true that fread() in general might return smaller than the requested size so Yes theoretically the original code was incorrect.

Connectez-vous pour commenter.

Isaac
Isaac le 29 Déc 2017

0 votes

I've ran into this too (in Matlab 2016b) and I got around it by:
tempNum = fread(fileID, 1, '*uint8');
fixed_size_matrix(ii,jj) = tempNum(1,1);
The way you wrote the code should work as is instead of needing this intermediate variable, but this works as a temporary solution.

1 commentaire

You could also then use
if isempty(tempNum)
to check for EOF. Checking EOF is in general a good idea: it is better not to assume that your file cannot have been corrupted somehow (including by running out of disk space for example.)

Connectez-vous pour commenter.

Catégories

Question posée :

le 23 Mar 2017

Commenté :

le 29 Déc 2017

Community Treasure Hunt

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

Start Hunting!

Translated by