fread size mismatch in Code Generation

11 Ansichten (letzte 30 Tage)
offroad
offroad am 23 Mär. 2017
Kommentiert: Walter Roberson am 29 Dez. 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?

Antworten (2)

Nagini Venkata Krishna Kumari Palem
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 Kommentar
Walter Roberson
Walter Roberson am 29 Dez. 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.

Melden Sie sich an, um zu kommentieren.


Isaac
Isaac am 29 Dez. 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 Kommentar
Walter Roberson
Walter Roberson am 29 Dez. 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.)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by