fread size mismatch in Code Generation

조회 수: 4 (최근 30일)
offroad
offroad 2017년 3월 23일
댓글: Walter Roberson 2017년 12월 29일
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?

답변 (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
Walter Roberson
Walter Roberson 2017년 12월 29일
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.

댓글을 달려면 로그인하십시오.


Isaac
Isaac 2017년 12월 29일
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
Walter Roberson
Walter Roberson 2017년 12월 29일
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.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by