fread size mismatch in Code Generation

11 visualizaciones (últimos 30 días)
offroad
offroad el 23 de Mzo. de 2017
Comentada: Walter Roberson el 29 de Dic. de 2017
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?

Respuestas (2)

Nagini Venkata Krishna Kumari Palem
Nagini Venkata Krishna Kumari Palem el 31 de Mzo. de 2017
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 comentario
Walter Roberson
Walter Roberson el 29 de Dic. de 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.

Iniciar sesión para comentar.


Isaac
Isaac el 29 de Dic. de 2017
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 comentario
Walter Roberson
Walter Roberson el 29 de Dic. de 2017
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.)

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by